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:

sudoapt-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:

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

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:

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.

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: