How to Write C# programs on a Raspberry Pi Using Mono

Since the Raspberry Pi is capable of running a full Linux distribution, then its potential as a development tool is enormous. Not only does the Pi support the mainstream compiled languages like C, C++  and Java, it also supports popular scripting languages such as Python, Perl and Lua. The Pi is also able to use some of the lesser known (but just as powerful) compiled languages such as Google’s Go language and C#. Using the latter on the Raspberry Pi is simple thanks to the open source Mono project.

Mono is a set of tools (including a C# compiler and the Common Language Runtime) used to create “.NET” compatible programs based on the published ECMA standards. In essence, it lets you compile and run C# code on Linux, and the resulting binaries are fully compatible with Microsoft.NET.

To install Mono on the Raspberry use:

sudo apt-get install mono-complete

The next step is to compile the standard “Hello World” program. Create a file called “hello.cs” using your favorite text editor. To create it with nano, type:

nano hello.cs

Cut and paste the following code:

using System;
 
public class HelloWorld
{
    static public void Main ()
    {
        Console.WriteLine ("Hello Make Tech Easier");
    }
}

C# is an object-oriented language, which means everything is defined within a class. The code above creates a class called “HelloWorld”, and defines a function within that called “Main”. Main() is always the starting point for C# programs. “Console.WriteLine” prints text out to the terminal (console).

To compile the program type:

mcs hello.cs

This will create a file called “hello.exe” in the current directory. Normally in Linux, binary files don’t use the “.exe” extension, it is something that comes from DOS and Windows. If you check the file type of “hello.exe”, you will see that it is actually an Intel executable:

file hello.exe

mono-file-hello

The output shows that “hello.exe” is a Mono/.Net assembly that was built to run on a PC with Windows, using an Intel processor. But we compiled it on Linux, using an ARM processor!

This is because .NET actually uses an an intermediate language which is interpreted by the Common Language Runtime. The beginning of the .exe file is actual Intel/Windows code which automatically starts the runtime and allows the rest of the program (as intermediate code) to run.

The official way to start the program on a Raspberry is via the “mono” command:

mono hello.exe

And as you would expect, the output will show “Hello Make Tech Easier” in the terminal. Because the output from Mono is binary compatible with Windows, you can copy the “hello.exe” file to a Windows PC and run it directly.

It is also possible to write GUI programs using GTK. But first you need to install the bindings between Mono and GTK:

sudo apt-get install gtk-sharp2

Create a file called “hellogtk.cs” with the following code in it:

using Gtk;
using System;
 
class Hello {
 
        static void Main()
        {
                Application.Init ();
 
                Window window = new Window ("Hello MTE");
                window.Show();
 
                Application.Run ();
 
        }
}

This is a very, very simple GTK program that will open a new Window. To compile it type:

mcs hellogtk.cs -pkg:gtk-sharp-2.0

The “pkg” flag tells Mono that this program needs to use the GTK toolkit. To run “hellogtk.exe”, you need to make sure that the desktop is running on your Pi. From either the file manager, or from a terminal started from the desktop, run the binary.

mono-hellogtk

A small window will open with the title of “Hello MTE.” The app doesn’t do anything else, but it shows that you can write GUI programs on your Pi using C#.

Mono has a cross-platform IDE sister project, known as MonoDevelop. The projects makes it easy to write desktop and ASP.NET Web applications on Linux, Windows and Mac OSX. It is also available on the Raspberry Pi. To install it, type the following in a terminal:

sudo apt-get install monodevelop

mono-monodevelop

If you get stuck using Mono, then the project’s support page has links to the forums, mailing lists, as well as the FAQs. MonoDevelop also has comprehensive documentation and a FAQ page.