Developing Cross-Platform C# Applications with Mono

Introduction

Building cross-platform software has been a pain in the side of C# developers since the early days of computing. While it certainly has gotten much better, we still aren’t quite to the point of one executable running as-is on Linux, Mac OS X and Windows. That is unless you consider the Mono toolset from Novell and the fact that Mono does support all three major operating systems.

The biggest interest these days in moving from one OS to another would have to be from Windows to either Linux or Mac OS X. Graphical User Interface (GUI) applications, which include most desktop applications, present the biggest challenge due to the inherent differences in the Application Programming Interface (API) for each OS. Linux complicates things even more when you consider there are at least two major desktop environments, GNOME and KDE.

That’s not to say you can’t take an application developed on Linux and run it on Windows. In fact, the Monodevelop application uses Gtk# for all its GUI elements, and it runs on all three platforms. To make this work you must install the Gtk# platform-specific library. Monodevelop includes an integrated Gtk# visual designer called Stetic. It works on Linux and Windows, enabling you to build cross-platform Gtk#-based applications.

C# Programming and Windows Forms

The Mono team figured out early in the project that to make their tool truly cross-platform they would have to support Windows forms-based applications as much as possible. To make this a reality the team essentially developed an interface between Cocoa on OS X and GNOME on Linux. The runtime library takes care of all the translation between the calls in the application and the actual native API on the target platform.

You’ll need to install the Mono runtime on each platform in much the same way you would install the appropriate .NET framework runtime on different versions of Windows. Mono:OSX includes support for both Windows.Forms and Gtk# GUI toolkits along with the Mono runtime, SDK, documentation and the Monodevelop IDE. Supported Linux distributions include openSUSE, Novell SLES/SLED and Sun/Oracle Solaris. Unsupported or community supported distributions include Debian and Ubuntu.

Support for building Windows Forms applications with Mono tools is limited to what you can do programmatically. That being said, it really was the intention from the beginning to provide support for running applications developed on a Windows platform on Linux and OS X. Microsoft Visual Studio tools deliver a fantastic environment for creating user interfaces visually, writing the code and then debugging the application as a whole. What the Mono team does provide is integration directly into Microsoft Visual Studio with their MonoTools solution.

Tooling

Novell today supports a number of different options for developing with Mono from the open source Monodevelop tool to Visual Studio integration. The Mono Migration Analyzer (MoMA) will examine an existing Windows application in an attempt to identify any potential cross-platform issues. MoMA will scan compiled assemblies, looking for platform-dependent code like user32. It will also identify any feature that currently isn’t supported in Mono. The first beta of MonoTools version 2 for Microsoft Visual Studio has just been released with support for Microsoft Visual Studio 2010 and a new soft debugger capability.

Monodevelop is itself a cross-platform application with versions for Linux, Mac (Figure 1) OS X (Figure 2) and Windows (Figure 3). Early versions of Monodevelop were derived from the open source Sharp develop project. Monodevelop is also the primary tool for developing applications for the iPad, iPhone and iPod Touch using Monotouch. A version for the Android platform is currently in the beta test phase. The newest release of Monodevelop also currently in beta includes a number of tools to help make testing even easier.



Figure 1



Figure 2



Figure 3

Monodevelop includes a feature for extending the tool through the use of add-ins. The Mono team uses this technique to provide support for iPhone development in their MonoTouch product. Moonlight is the Linux equivalent of Microsoft’s Silverlight technology and is also supported through an add-in in Monodevelop. Additional add-ins include support for other languages such as Boo, Python, and Vala. You’ll find Monodevelop has integrated version control support and unit testing with NUnit as well.

Wrapping Up

Mono provides the closest thing currently available to the vision of a truly cross-platform executable. In many cases you can take the exact same executable file and run it on any supported OS. While that doesn’t work for every application, it does represent a huge step toward that goal. Developers get to continue using their tools of choice, such as Microsoft Visual Studio, without the need to learn a completely new way of coding.

For Independent Software Vendors (ISVs) Mono offers a way to make their products available on any platform for a fraction of the cost it would have been previously. While there may still be some features or functions that require platform-specific coding, the amount of code necessary should be minimal. In the next article we’ll take a look at porting an existing C# database application from Windows over to Linux and Mac OS X.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read