Getting Started With C# On Linux


When Microsoft launched its .NET strategy, one of the objectives was toallow software to be written for a variety of different platforms. Itsubmitted .NET to ECMA for standardization and many people now considerit to be more "open" and accessible than the Java platform.

Miguel de Icaza, the founder of Ximian is working on Mono – an opensource implementation of the .NET framework which runs under Linux.

If, like me, you work with Microsoft technologies, but also tinker withLinux, then you can now write C Sharp programs which run on bothplatforms. This article takes a brief look at Mono and explains how toinstall the software and start some simple programming.

Head over to www.go-mono.com and download the latest packaged version ofMono. At the time of writing this is mono-0.10.tar.gz

Before you try to build Mono, you need to make sure that you have fairlynew versions of the GIMP Toolkit and Drawing Kit, as well as pkg-config.I have a RedHat 7.2 system and I still had to download and upgrade thesepackages. You can get them from rpmfind.net and install them as follows:

# rpm -Uvh glib2-2.0.0-1.i386.rpm
# rpm -Uvh glib2-devel-2.0.0-1.i386.rpm
# rpm -Uvh pkgconfig-0.12.0-1.i386.rpm

The next step is to unpack the Mono distribution file like this:

# tar -zxvf mono-0.10.tar.gz

To install Mono, change into the newly created mono-0.10 directory andtype

# ./configure
# make
# make install

If all goes well, then you should now have a working Mono systemincluding mcs (The Mono C Sharp Compiler suite) , mono (The Mono Just inTime compiler) and mint (The Mono interpreter). All of which now haveuseful man pages.

Tradition dictates that the first program we should try out is thefamous Hello World, and here it is coded in C Sharp.

// Hello World in C Sharp
class Hello {
static void Main() {
System.Console.WriteLine("Hello World");
}
}

C Sharp programs must end in the .cs suffix. Type this file in (usingyour favourite text editor) and save it to a file Hello.cs then you cancompile it using the following command

# mcs Hello.cs

If you've typed everything correctly and there are no syntax errors,this should generate a MSIL file called Hello.exe which you can try outas follows:

# mint Hello.exe
Hello World

The mint interpreter has a number of options including –trace and–debug which are useful for debugging. When your program is finished,you can run it with the JIT compiler to get full performance.

# mono Hello.exe
Hello World

The Common Language Runtime provides cross-platform portability. A .NETapplication can run on any system for which the CLR has been ported. Infact Mono version 0.10 is the first version to be "self hosting" beforethat, the Mono C Sharp compiler had to be compiled using the Microsoft.NET Framework SDK on Windows and then moved over to Linux. You can takeC Sharp programs compiled on Windows and run them on Linux, but at thetime of writing there are still a few problems going from Linux toWindows.

Building GUI applications under Mono is still difficult, but work isunderway. The Gtk# project at gtk-sharp.sourceforge.net aims to provideC# language bindings for the gtk+ toolkit. The aim is also to provide aWindows.Forms compatible library under Mono. There are several areas,where moving from Microsoft platforms to Linux may be hard, inparticular where Microsoft rely on win32 extensions, HWND and HDChandles etc, but much of this has already been thought through by theteam.

Mono is not yet a full implementation of the .NET framework, but thereis enough there to start writing some interesting programs. New releasesare likely to come thick and fast, and the breadth and quality of thebase classes will continue to improve.

Miguel de Icaza has hinted at using Mono for GNOME development. MaybeMono and .NET will help to bring the open source and Microsoftdevelopment communities closer together?

Useful Sites
www.go-mono.com The home of the Mono project
msdn.microsoft.com/net Microsoft .Net Development
www.aws.netActive Web Solution

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!