Meet the Monkey: A .NET Programmer’s Introduction to Mono
Are you curious about cross-platform .NET applications? Do you have a need or an interest in how to write .NET applications that will run on OS X? Do you need to deploy your application to a *nix box? If you’ve answered “Yes” to one of these last two questions or are curious about running .NET code on *nix, then read on.
With this article, I’ll introduce Mono along with some advice and tips on how to write cross-compatible code.
What Is Mono?
Mono is an open-source *nix version of the .NET development platform. I’ve taken this description of Mono from the Mono Web site:
“The goal of Mono is to allow .NET developers to create and deploy cross-platform versions of their applications.”
As you start off with Mono, one thing that might confuse you is the version numbers. Don’t make the mistake of assuming that the current Mono version number maps to or matches any particular .NET Framework version number. For example, Mono 2.2 (the current version at the time of writing), is somewhere between .NET 2.0 and .NET 3.5. Odds are that you won’t have too much trouble running .NET 3.5 code. However, if you are using some of the .NET 3.0 features (WPF, Windows Workflow, WCF) then you will begin to experience discomfort because these .NET 3.0 features either do not exist or are only partially implemented. On the other hand sometimes Mono comes out with .NET features before .NET: Mono 1.2.6 had support for some .NET 3.5 features such as lambdas before .NET 3.5 was released. Or, if you need to use the C# compiler as a service, that is available now from Mono.
What is currently implemented and what is going to be implemented is constantly changing. For specific details, consult the Mono roadmap at http://www.mono-project.com/Roadmap.
Mono has been allowing developers to write and deploy cross-platform .NET applications since June of 2004. One of the handy things about Mono is that it is binary compatible with .NET. This means that in many cases you can compile your applications with the .NET Framework, and then deploy them to any Mono-supported operating system and hardware combination. Likewise, you could compile your application using Mono, and then deploy to the .NET runtime.
Places You Can Use Mono
What exactly is meant by cross platform? Mono supports both 32-bit and 64-bit systems and a wide variety of operating systems. Mono currently supports the following operating systems:
Mac OS X (and the iPhone)
OpenBSD, FreeBSD, NetBSD
Mono will run on the following hardware:
Sun SPARC (32 bit)
s390, s390x (32 bit and 64 bit)
In addition to the architectures listed above, it is possible to make your own smaller Mono install for embedded systems, if you find it necessary. It is beyond the scope of this article to go into this topic in detail, but you can find more about this by visiting the Mono Web site at http://www.mono-project.com/Small_footprint.
Of these four classes of issues that MoMA will find for you, System.MissingMethodException is the most oppressive. These exceptions will actually prevent your application from loading and running.
Getting Started with Mono
How do you get started with Mono? To begin with, you need to install it.
First, point your Web browser to the download page for the Mono project (http://www.mono-project.com/Downloads). From there you will be presented with several choices detailed below:
- Use the LiveCD. The LiveCD is a good choice if you just want to test the waters. At the time of this writing, the Mono LiveCD is running openSUSE 11.1 with Mono 2.2 and sample applications. The LiveCD will give you the best performance as it is running on your hardware. The drawback is that you will have to take some extra steps to persist your data-saving to a USB drive for example.
- Use the VMware Image. This technique is arguably just as simple (or simpler) than using the LiveCD. Download the VMware virtual machine and open it up using VMware’s free VMware Player. Once the virtual machine boots up, you are basically running the same environment that you would have if you would have gone with the LiveCD approach.
- Use the Installer for your Operating System. This is probably the simplest option. Just download the appropriate installer for your OS and make use of that.
- Build from Source. Being that Mono is open source, it is possible to get the source code for Mono and build it yourself. I won’t go into this option in this article, as building Mono from source and installing isn’t necessarily for the faint of heart, particularly on Windows. However, if you have specialized needs or want to hack the Mono source, this is yet another option for you.
For the purpose of this article, and for most users, any of the first three options mentioned above is more than sufficient.
If you are unsure how to get a copy of Mono to experiment with, ask yourself this question first:
How familiar am I with UNIX?
If you are comfortable working in UNIX (specifically Linux), then you might just want to use the VMware image or the LiveCD. This will give you a completely isolated sandbox and you won’t have to worry about “cluttering” up your workstation with something that you are only experimenting with anyway.
If you prefer Windows or are not comfortable with UNIX, then you might just want to use the Mono MSI download to install Mono on your PC.
With the details of how to install Mono on your computer out of the way, let’s move on to a bit about some of the tools you can use to craft your Mono applications.
By: Tom Opgenorth
Tom Opgenorth is an independent consultant residing near Edmonton, Alberta, an MVP in C# for 2008, and an all-around good guy (just ask him) with a taste for single malt Scotch and cigars. Tom has been developing software in various languages and on a variety of platforms for the last fourteen years, focusing primarily on .NET for the past six of those years.
Tom discovered the Mono project back in 2003 when trying to figure out how to leverage his .NET skills on Linux, and has been hooked on Mono ever since.
Why the monkey? You may have notice that the mascot for Mono is a monkey. The name Mono is in fact derived from the Spanish word for monkey. There really isn’t a significant reason for this, it just is.