The C# language is an object-oriented language designed to let you quickly build a range of applications for the Microsoft .NET platform. The goal of C# and .NET is to free you from low-level programming issues -- type-safety issues, memory management, library construction, etc. -- in order to allow you to concentrate on building your applications and focus on business logic. This, in turn, should help shorten the development cycle.
With all the buzz around the C# and .NET strategy, some might wonder what the Linux community is doing in reaction. This article discusses how the open source world (with strong backing from Novell) has chosen to embrace the language by implementing its own compiler and class libraries. (In fact, several open source implementations are underway.)
This article focuses on Mono, the open source development platform based on the .NET framework and sponsored by Novell. This article takes you through installing Mono on your system and gets you started developing your first sample C# application, compiled with Mono, that runs on both Microsoft Windows and Linux.
Mono gives open source developers the programming power and flexibility to build applications for Linux while maintaining cross-platform capabilities, using a variety of .NET-compatible languages. One of the great advantages of Mono for current .NET developers is providing an easier migration path to Linux. The Mono project has a very open and active development community and provides both developer tools and the infrastructure needed to run .NET client and server applications.
Perhaps the most important benefit of using the Mono architecture is that you gain language independence. Mono lets you leverage any existing code from languages supported in the .NET runtime.
Consider the following: traditional Linux programming is often done entirely in C. Normally, when you want to use another language, such as Python, you need to use the Python bindings for the native C code.
This approach, creating bindings, is not always optimal or easy to do, and in some cases, no bindings exist; therefore, from any one Mono-supported language, Mono allows you to access existing code from any other Mono-supported language. You can write applications in C#, Python, or even Java™ while reusing classes from the other languages without the need for bindings! (An example of using C library code from a C# program via the Platform Invocation Facility [pinvoke] is a bit later in this article.)
As mentioned above, your code also gains platform independence. This happens automatically because .NET code is not compiled to machine code; rather, it is compiled to intermediate language (IL). The IL used in the .NET runtime that is provided by Mono makes it possible to copy binaries across platforms. This idea is quite familiar to Java developers -- it echoes the concept of "write once, run anywhere."
Mono also provides a time-saving free implementation of the .NET framework class library. This library is already written and tested code designed for a variety of common programming tasks (as well as for some that are not so common). The library includes most common data structures, cryptographic libraries, XML, and Web services support.
Though many languages can be used within the .NET framework, C# is the one held in highest esteem by .NET developers. C# is an effective, garbage-collected language that provides powerful object-oriented capabilities and allows you to get to the core of your programming tasks quickly. The language has a syntax familiar to almost anyone who has programmed in a modern C-derived language, but includes convenience features familiar to those using modern scripting languages as well.
Lastly the Mono JIT (Just In Time) compiler is speedy, providing all these benefits at a reasonable performance level.
The Mono project includes the following open source implementations:
- A C# compiler
- The Virtual Execution System, including a JIT compiler, garbage collector, loader, and threading engine
- An implementation of the .NET class library
- Visual development tools and debuggers
The Mono project's tools can be run on a variety of hardware and operating systems, including Linux, Windows, and Mac OS X.
The Mono project currently supports the following list of languages to varying degrees:
- C#
- Java
- Boo
- Nemerle
- Visual Basic.NET
- Python
- JavaScript
- Oberon
- PHP
- Object Pascal
- And more
Note that not all of these are as complete as the C# runtime. For example, Java isn't supported directly but uses on-the-fly translations from Java bytecodes to IL.
Another example of the differences in the implementation completeness is that the Python implementation seems to have stalled recently. Developers seeking python might want to use Boo, which is a more complete scripting language implementation (while being similar to Python and designed for the .NET framework). While discussing Mono-supported languages, I should mention that Nemerle is a new hybrid programming language (functional, object-oriented, and imperative) for the .NET platform.
Some people are confused as to why open source advocates would attempt to take a Microsoft product and embrace it. In fact, many wonder why Microsoft would allow this in the first place.
The reason this is possible, and as it turns out even encouraged, is that Microsoft has followed through on its promises to standardize C# and the CLI. Because of that standardization and specification, the Mono team has been able to construct their own open implementation of the ECMA standard for C# and the Common Language Infrastructure. In fact, the compiler and language themselves are openly specified for others to implement as well.
A lot of the fuss about Mono arises more from the discussion of the provided class libraries that are not necessarily covered as an open specification. This invariably leads to flamewars about patents and whether or not Microsoft will try to use patents to attack Mono as part of a broader strategy of attacking Linux. To that end, the Mono developers provide two independent sets of libraries.
One library set is a reimplementation of the Microsoft .NET libraries used for compatibility with Microsoft .NET applications. This includes ADO.NET, System.Windows.Forms, and ASP.NET.
The Mono developers have also created their own powerful stack of libraries from the ground up, based on existing open source libraries and technologies with the intent of providing functional parity while being free from any concerns of patent infringement. For example, Mono ships with a windowing technology called Gtk#, a set of C# bindings for the gtk+ toolkit, and assorted GNOME libraries. This enables application developers to write fully native GNOME application using Mono. You can consider this library the open source parallel of Microsoft's System.Windows.Forms library.
What platforms does Mono run on?
Mono has been successfully ported to Linux on multiple hardware platforms, FreeBSD, Windows, and Mac OS X. It is estimated that this covers 99 percent of the desktop market.
| Platform | Interpreter | Jitter (faster) |
|---|---|---|
| Linux/Alpha | Yes | No |
| Linux/ARM | Yes | Yes |
| Linux/MIPS | Yes | No |
| Linux (PPC) | Yes | Yes |
| Linux (S390) | Yes | Yes |
| Linux/SPARC | Yes | Yes |
| Linux (x86) | Yes | Yes |
| Linux/x86-64 (64-bit port) | Yes | Yes |
| FreeBSD (x86) | Yes | Yes |
| HP-UX/HPPA | Yes | No |
| MacOS X | Yes | Yes |
| Solaris/SPARC v8 | Yes | Yes |
| Solaris/SPARC v9 (64-bit port) | Yes | Yes |
| Windows 2000/XP | Yes | Yes |
Now, let's look at some of the nuances in installing Mono.
To install Mono on the Windows operating system, you need to be running Windows 2000 or Windows XP. To begin, download the latest editions of the Mono Win32 installer from the Mono Windows Integration Project home page hosted at NovellForge (see Resources at the end of this article). When the download has completed, run the executable installer.
The installation process is similar to other product installs. Initially, you will be asked to accept or decline a license. After accepting, you need only provide the installation path and select any optional components.
For the purpose of learning the Mono frameworks, it is recommended that you use the default installation, which provides start menu entries for the common tools. These are categorized under a new "Mono-1.1.7 for Windows" program group.
Installing Mono for Linux should be relatively easy in most cases (the specifics of obtaining packages for each and every distribution is beyond the scope of this article).
Binary installation packages are readily available for SUSE, Red Hat, and Ubuntu, as well as many others. Source-based distributions like Gentoo are also easy to get up and running with minimal effort.
To test the core compiler (mcs) as well as the runtime (mono), you should create a simple program and compile it. You may create the program in any text editor you prefer. For a quick and dirty way of creating this file (albeit without any elegant formatting), run the following command from your terminal prompt (all on one line):
$ echo 'class X { static void Main () { System.Console.Write("My first.
mono app worked!\n");} }' > example.cs
This command creates a C# source file called example.cs (you can also download this file, example.cs, and the executable, example.exe from Downloads, below). (Note that if you are using Linux, you may simply use your bash prompt; if you are using Windows, invoke the Mono command prompt from the start menu.)
To test the compiler's ability to create executables, invoke the following command:
$ mcs example.cs
This should leave you with a binary called example.exe. To run this binary and thus test the runtime, use this command:
$ mono example.exe
If all went well, you should see "My first mono app worked!" in your console with no errors.
Figure 1. What you'll see if all went well

In fact, you can copy the resulting executable file to another system, perhaps one running Windows, and then execute it there, unmodified.
Using code from a non-Mono library
One of the more compelling reasons to use the Mono platform is the ability to use code from an existing, possibly non-C#, library from your C# code. Here is an example of doing just that (you can also download Listing 2, PInvokeExample.cs, and the executable, PInvokeExample.exe, from the Downloads section, below).
The mechanism that provides this capability is the Platform Invocation Facility (or pinvoke, for short).
Listing 2. Using the Platform Invocation Facility
/* Platform Invocation Facility Example
This code is intended to illustrate P/Invoke.
For out purposes we will access a c shared library.
*/
using System;
// This is a lot of the magic!
using System.Runtime.InteropServices;
/* Class illustrates pinvoking existing c library code */
public class PInvokeExample
{
/* Notifying the runtime that we need an
additional mathematics library, libm.so */
[DllImport("libm")]
/* Here we define the external function we will
be using from the library,
You must declare these methods to be static.
The function signature you provide must match the
signature in the external library!
*/
static extern double sqrt ( double element );
public static void Main ()
{
Console.WriteLine("The square root of 100.0 is {0}.", sqrt(100.0));
}
}
|
As you can see in the simplified code, you need only direct the Mono compiler to use the library (this was done with the DLLImport line) and provide the prototype for the function you wish to use. If you compile this class on a Linux system, the console output should show the correct answer.
Figure 2. Using non-C# libraries

Mono's runtime can also be embedded into applications for simplified packaging and shipping. In addition, the Mono project offers an integrated development environment, debugger, and documentation browser.
If you are interested in learning more about C# and the .NET framework, a variety of Resources are listed below, including references to various integrated development environments like Eclipse and Monodevelop to further simplify your development process.
If you are interested in developing graphical applications with Mono, I highly recommend "Mono: A Developers Notebook" as a more in-depth tutorial (Resources).
The programmers at Novell are continuously adding new functionality to Mono and providing tools to make open source developers' lives easier. I hope that this article gives you with a solid introduction to the Mono project and that you can use it in your next Linux development project.
| Description | Name | Size | Download method |
|---|---|---|---|
| Installation test source | example.cs | 1 KB | HTTP |
| Installation test executable | example.exe | 3 KB | HTTP |
| PInvoke sample source | PInvokeExample.cs | 1 KB | HTTP |
| PInvoke sample executable | PInvokeExample.exe | 3 KB | HTTP |
Information about download methods
Learn
-
Learn more about Standard ECMA-334, the C# Language Specification.
- Got Mono is the home for the Mono community.
-
The Mono Documentation Library includes documentation on Mono class libraries.
-
If you're considering making the jump to the Java language, read "Getting on the open road: J2EE fundamentals for .NET developers" (February 2004), the first of a three-part series helping .NET developers take the plunge.
-
"Securing Linux for Java services" (developerWorks, April 2003) includes a section on C# and the Mono Project.
-
"Implementing a .NET Managed Provider for DB2 Universal Database" (developerWorks, May 2003) provides more information on .NET technology and Mono.
-
The "Monodoc Tutorial" gives guidance for developing Mono documentation.
-
"C#, the natural progression" (developerWorks, August 2000) demonstrates C#'s place in the language hierarchy.
- Mono: A Developer's Notebook (O'Reilly, July 2004) is a no-fluff, lab-style guide into Mono 1.0 with 50 mini-projects.
- Mono Kick Start (Sams, September 2003) is a practical introduction to Mono and .NET-compatible application programming with Mono.
-
The Mono Project languages page gives the long and short of Mono-compatible languages.
-
Find more resources for Linux developers in the developerWorks Linux zone.
Get products and technologies
-
Download Mono for various platforms from the Mono Project downloads page.
- Eclipse for developing Mono applications gives information on using the Eclipse IDE for C# and other Mono languages.
- DotGNU provides free software solutions for Web services and C# programs.
- MonoDevelop is a project to port SharpDevelop to Gtk# to create a best-of-breed development environment for UNIX systems for C# and Mono.
- Gtk# contains a set of C# bindings for the gtk+ toolkit and assorted GNOME libraries.
-
The Mono Windows Integration Project is developing Mono and its tool set for use on Windows.
-
Build your next development project on Linux with IBM trial software, available for download directly from developerWorks.
Discuss
-
To get in touch with the Mono community, check out the Mono community forums.
-
Get involved in the developerWorks community by participating in developerWorks blogs.
Eli Dow is a Software Engineer in the IBM Linux Test and Integration Center in Poughkeepsie, NY. He holds a B.S. degree in Computer Science and Psychology and a Masters of Computer Science from Clarkson University. His interests include the GNOME desktop, human computer interaction, and Linux systems programming. You can contact Eli at emdow@us.ibm.com.



