Welcome to my tutorial on beginner cross platform development in C# with Mono. Mono is an open source implementation of the .NET Framework based on the ECMA standards for C# that works on most common platforms. As a ‘NIX developer, I am a big fan of the C family of languages. C# is a logical evolution in the C family, that has much of the power of C and C++, without all of the malloc and calloc. I have often said that C# is one of the few good thing that have come out of Microsoft.

Bias Alert!!:
I am a Mono developer not a .Net developer, the difference is subtle (Blasted carriage return line feed (“\r\n”) line endings).

A little about C#

“C is quirky, flawed, and an enormous success.” ― Dennis M. Ritchie

C# is a strongly typed, modern, object oriented programming language developed my Microsoft in the early 2000s. It was developed as an update to the C family (C, C++, OOC) of languages. When built, C# is usually compiled to an intermediary language, such as Common Language Infrastructure (CLI) code. There are a couple other compiling options out there, but they are not for the faint of heart (keep a lookout for future articles about that). Once compiled into the intermediary language, the application is run on a virtual machine (.NET, Mono, LLVM).

Traced by User:Stannered (en:Image:Mono architecture.png) [GFDL or CC-BY-SA-3.0], via Wikimedia Commons

Compiling C#

Most of the time C# is compiled in a two step process. In step one, it is compiled into a common intermediary language and then Just In Time (JIT) compiled at run time by the virtual machine. This allows the compiler to run optimizations at runtime, automatic memory managment (garbage collection), dynamic types, and many other benifits. Like everything else JIT has it’s costs. Some of the cons of JITing is that it increases application start load time, lack of control of memory managment, larger application size, and incresed complexity of the virtual machines.

How is that different then C/C++ or other compiled languages?

In essence C/C++ is compiled directly to a platform (x86, Arm, …) specific executable binary. In order to run on multiple platform you must recompile the program for each platform. Compiled languages have the advantage of more or less total control of the memory allocation/deallocation, which means they CAN have better performance than virtualized languages like C#, but that leaves the entire responsibility of memory management to the developer.

C++ Archetecture

What about parsed languages like PHP?

Many of the parsed languages are migrating towards virtual machine (HHVM). The truly parsed languages are run through a parser, instead of a virtual machine. Parsing maps commands to pre-compiled, rather than compiling them into a lower level language (machine code, etc).

Hello World

Let’s jump right into the mandatory “Hello World” application. For this tutorial I will am using Xamarin Studio on a Mac. The example should work on any C# compiler/IDE. Open up you IDE of choice and create a new command line application. If you are are using Xamarin Studio you should have a file called Program.cs that contains something like:

//Program.cs
using System;

namespace HelloWorld
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Hello World!");
        }
    }
}

At a minimum your app will need a class that can have any name with a public static method named Main. Each program can only have one public static method name Main.

Running your first C# program

To run the program you can use the IDE’s run command or build it, then run it from command line with HelloWorld.exe for DOS/Windows system or mono HelloWorld.exe for everything else. If you are developing for non-DOS/Windows system you can write a script such as the following to make it easier to run your program.

#!/bin/sh
/usr/bin/mono /usr/lib/APPLICATION/myprogram.exe "$@"

Much like a standard C/C++ program the main method is passed an array of strings. The array contains the parameters passed to the program when run. For example, if you run the above program with HelloWorld.exe me, the value of args[0] will be “me”. To play nicer with ‘NIX OSes you could return an integer exit status like public static int Main (string[] args). Most systems have a variety of exit codes available, but generally 0 for successful completion and 1 for error are all that is needed.

//Program.cs
using System;

namespace HelloWorld
{
    class MainClass
    {
        public static int Main (string[] args)
        {
            Console.WriteLine ("Hello World!");

            return 0;  //Exit status sucess
        }
    }
}

If everything went well you should have a program that prints Hello World! and exits. Thanks for sticking with me this far. Until next time, keep Hackin’.