Preface

Mono is an open source, cross-platform, implementation of the .NET development framework. It’s based on the Common Language Infrastructure (CLI), standardized by the ECMA standards group and ratified as standards by ISO. Similar to Java, the Mono environment consists of a compiler, virtual machine, and API classes. Although .NET supports many languages, Mono’s main compiler focus is on the C# language.

This book is for developers who want to get up to speed quickly with Mono. Light on theory and strong on no-nonsense code examples, this book will lead you through what you can do now with Mono and provides plenty of jumping-off points for finding out more. By working through a series of labs, each centered on a particular API, task, or feature, you will quickly obtain running code that can be used as a basis for further learning or evaluation.

What This Book Covers

As well as shipping with the basic ECMA standardized APIs, Mono comes with two sets of class libraries (known as assemblies in the .NET world): Microsoft compatibility APIs and Mono’s own API set. The compatibility assemblies include ADO.NET for databases, ASP.NET for web applications, and Windows.Forms for desktop applications. The Mono assemblies include among other things the Gtk# user interface toolkit, interfaces to databases such as MySQL and PostgreSQL, and an interface to the Mozilla web browser.

This book is mainly about the standard ECMA libraries and those unique to Mono. It will lead you through discovering the following aspects of Mono:

Installation and configuration

Chapter 1 discusses setting up the Mono development platform on Linux, Mac OS X, and Windows, and creating a functional development environment with your tools of choice.

Mono programming basics

Chapter 2 provides a rapid tour of the major features of the C# programming language, and Chapter 3 introduces the basics of the system class libraries, including file IO, string handling, regular expressions, threads, and unit tests.

User interfaces

The preferred user interface toolkit for Mono is Gtk#. Chapter 4 and Chapter 5 cover programming with Gtk#, and include some more advanced widgets targeting the GNOME desktop environment.

Processing XML

XML is a major feature of modern software development, and Mono’s System.Xml classes provide a rich variety of APIs for XML-related programming. These are discussed in Chapter 6.

Networking

Mono comes with a rich set of features for creating networking applications. The ASP.NET libraries are a framework for web applications and web services. Chapter 7 discusses ASP.NET, along with more conventional TCP/IP and HTTP applications, encryption, remote procedure calls and database access.

Advanced and experimental topics

Chapter 8 discusses packaging and distributing Mono applications using GNU automake and autoconf, running Java and Basic programs in Mono, writing cross-platform code, and using generics in C#.

The aspects of Mono not covered in this book include the Microsoft compatibility assemblies. In particular, the Windows.Forms user interface libraries are not covered. O’Reilly publishes various books that cover these topics well. For more information on these titles, visit http://dotnet.oreilly.com/.

Why Mono?

Mono brings together many modern programming language features. While many of its technologies can be found in various alternative languages, it’s the conjunction of them in a single platform that makes Mono attractive and exciting to develop with.

Provides a controlled environment

One of the main benefits of Mono is that programs are compiled into a bytecode known as the Common Intermediate Language (CIL) and run in a controlled environment; such code is known as managed code. This managed environment provides many advantages, some of which will be familiar already to users of Java or scripting languages.

No crash and burn

In traditional compiled environments, buggy code generates nasty errors and unexpected program termination, often complicated by misbehaving code scribbling over parts of the memory. Mono’s exception handling and virtual machine environment means that errors can be isolated easily, and need not be fatal to a program.

Garbage collection

Memory management is the biggest headache for non-garbage-collected languages like C and C++. The freedom and perceived speed of compiled C is often outweighed by the headache of tracking down memory leaks. The Mono runtime takes the trash out so you don’t have to. In an ideal world, memory management done by hand is faster than automatic memory management. In reality, automated garbage collection is as fast as manual.

Runtime checking

The managed environment won’t let a program write beyond the end of an array, access a null pointer, or many of the other common causes of errors in nonmanaged programs. Instead, exceptions are used to allow the programmer to handle errors gracefully.

Security benefits

A comprehensive security model means that the access rights of programs to system and remote resources can be strictly controlled. Additionally, the runtime error checking minimizes the chances of a misbehaving program presenting opportunities for exploitation.

The escape hatch

For legacy integration reasons, developers usually need access to “unsafe” code and unmanaged structures. The runtime enables developers to incorporate such code, while still using a sandbox or disabling execution for untrusted code paths in certain environments.

Reuses existing code investments

Mono makes it easy to reuse existing investments in code libraries. The Platform Invocation Services, or P/Invoke, method allows access to almost any function in an existing binary library with just one or two extra lines of code. Java has a similar mechanism, but Mono makes it much simpler.

For access to Java libraries, the IKVM compiler ensures a smooth transition. IKVM enables Java bytecode to run in the Mono runtime, allowing access from Mono code to Java classes, and vice versa. Even highly complex Java applications such as Eclipse can be run on Mono using IKVM.

Fast to write, fast to run

In a world where computers are cheap but developers expensive, reducing development time is often much more important than reducing execution time. Developing with Mono and C# can bring significant productivity increases. C# can be quickly understood by programmers coming from either a Java or C++ programming background, and presents enough of the advantages of both of those environments to feel immediately comfortable. At the same time, C# adds some new language features that reduce the verbosity of programs, including:

Events and delegates

Events are first-class constructs in C#. Together with delegates (similar to function pointers) they make event-based code such as a user interface easy to write.

Properties

No more getFoo( ) and setFoo( ) methods. C# has syntactic sugar to allow properties to be accessed like variables, and also allows complex code behind the accessors.

For programmers moving from a C or scripting background into Mono, the benefits of a strongly and statically typed language can have a big impact on the development cycle. In a statically typed language, many errors are caught at compile time before they ever make it into a program. These advantages also translate well into team development settings, where formal interfaces between code modules are even more important.

Contrary to some expectations, CIL bytecode running in a virtual machine is not slow. The Mono runtime utilizes a just-in-time compiler (JIT) to translate bytecode into native machine code, maximizing application performance. Due to improvements made in the CIL design over Java bytecode, the Mono JIT is expected to match or exceed the speed of Java JITs.

Presents cross-platform code and migration paths

Unlike Microsoft’s .NET implementation, Mono is cross-platform, supporting Windows, Linux, and Mac OS X, and hardware including x86, PowerPC, and SPARC processors. The JIT means that not only will Mono run on these platforms but that it will perform well. In addition, the Mono platform libraries, such as the Gtk# user interface toolkit, are also available on all these systems, enabling single codebase deployment across multiple platforms.

Mono presents a smooth migration path for developers working on .NET in Windows. The implementation of ADO.NET, ASP.NET, and Windows.Forms means applications can be brought over to Linux as is. Running applications on Mono makes it easier than ever to deploy Linux with minimal risk or up-front investment. Due to platform-specific assumptions made by developers, it’s likely that some small porting effort will be needed, but occasionally the move will be as simple as copying over the compiled assemblies!

Provides a choice of languages

The common runtime is not restricted to C# alone. Java, JavaScript, Basic, C, and even Cobol have compilers available. Not all of these languages are statically typed like C#. The flexibility and expressiveness of scripting languages are also available. Importantly for many developers, a version of Python is being developed. Most likely, in the future, no major programming language will be without a compiler targeting the common runtime.

Works with familiar tools

Moving to Mono doesn’t mean that you must throw away the existing environments and toolchains that you are used to. For instance there are several build systems available, each from different cultures:

NAnt

A .NET port of Java’s famous Ant build system

Autotools

GNU autoconf and automake can be used easily with Mono, along with pkg-config

MonoDevelop

Projects can be built with this IDE, compatible with the popular Windows-based SharpDevelop IDE

Additionally, C# editing modes are available for all popular text editors. Mono doesn’t even impose any particular directory structure on the way source code is organized.

Provides a large community resources

As the basis for new Windows development, .NET has a substantial resource library available from MSDN and third-party publications. Given the wide array of complementary efforts in both the Microsoft .NET and Mono communities, developers won’t often find themselves without help.

In addition to developer education, open source development tools, such as testing frameworks, are readily available, and more are ported or written every day. Developers can expect to plug into a large community of peers, and the associated body of support and emerging best practice. Employers can take advantage of a large pool of available skills.

Conventions Used in This Book

This book uses the following typographic conventions:

Constant width

Used for program listings, classes, methods, variables, parameters, directives, keywords, and the output of command-line utilities.

Constant width italic

Used to show items that need to be replaced in commands.

Italic

Used for emphasis, for first use of a technical term, packages, channels, and URLs.

...

Indicates text that has been omitted for clarity.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Mono: A Developer’s Notebook by Edd Dumbill and Niel M. Bornstein. Copyright 2004 O’Reilly Media, Inc., 0-596-00792-2.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Comments and Questions

Please address any comments or questions concerning this book to the publisher:

O’Reilly Media
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the U.S. or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)

To ask technical questions or comment on the book, send email to:

O’Reilly has a web site for this book where examples, errata, and any plans for future editions are listed. You can access this site at:

For more information about this book and others, see the O’Reilly web site:

Acknowledgments

Edd Dumbill

Without Miguel de Icaza and the Mono team, neither Mono nor this book would exist. I owe them a great deal of thanks for all their work and enthusiastic help in solving the problems I came across while writing this book. In particular, my thanks are due to Miguel, Todd Berman, Gonzalo Paniagua, Mike Kestner, Erik Dasque, and Jackson Harper.

I’d like to thank Niel Bornstein for being such an excellent co-author and an inspiring companion through the rapid development of this book. Dave Beckett’s careful reviewing and comments have been invaluable, as has the input, encouragement, and cheerful optimism of our editor, Brian Jepson. I am indebted to Phil McCarthy for drawing the excellent monkey that adorns Chapters 4 and 5.

Finally, my thanks as ever to my wife Rachael, whose understanding, patience, and support has made this book possible.

Niel Bornstein

When Edd Dumbill approached me to cowrite this book with him, I jumped at the chance to write about the Mono project. Edd’s constant encouragement and support have made this an incomparable writing experience. Thanks also to our editor, Brian Jepson, who made the short deadlines easier to meet by providing excellent editorial support. Special thanks go to Dave Beckett for his invaluable commentary on the text.

Thanks also to Miguel de Icaza and his team, who have accomplished an amazing task in producing a great set of tools. In particular, thanks to Todd Berman, Erik Dasque, Atsushi Eno, and Gonzalo Paniagua for answering my questions about Mono’s inner workings.

Last, but by no means least, many thanks to my wife and son, Dawn and Nicholas, who have likely suffered more than anyone during this project. Thanks for your patience and encouragement, and I promise I’ll be in bed at a reasonable hour for a few months now.

Get Mono: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.