Oct 202010
 

For the last few months, I am almost exclusively using Ubuntu as my primary operating system. Due to this my .NET development suffered quite a bit and was limited just to work. To find a way around I installed Mono and played around with it.

Though a lot of people believe that .NET is completely tied with the Windows API, that is not exactly true. If we look at the source code of the .NET runtime (released by Microsoft under the name Rotor/SSCLI), there is a seperate layer called the Platform Adaption Layer (PAL) written which acts as an abstraction between the OS and runtime. This layer allows people to write their own implementations of the CLR to translate CIL into native code.

The mono project is one such effort which has ported .NET to both Mac and Linux environment. Even though Mono can work on Linux, I wouldnt really recommend using it for a production application because you might end up with bugs and small irritants while running it on linux which you can ill afford for a commercial application. So if you want to write once, run anywhere, Java is still your best bet.

To install Mono, first the repository needs to be added to the package manager. The mono packages are available here and a guide to add them is given here. After the source is added, click reload to update the list. The use the familiar apt-get command to install Mono. If that doesnt work enable the multiverse repositories or you can use the GUI Synaptic package manager.

Now mono is the application which can execute a .NET exe file. It however cannot compile the code. For that there is another application called mcs. To install it use the following command.

Now for the obligatory hello word program. Just write a simple program and save it as a text file.

Now lets compile it using mcs. The -v switch gives more detailed information in case a parsing error occurs.

If you observe the folder, you will see an exe file created with the same name as the source code file. Now, Linux has no idea how to run an exe without a compatibility layer (cough: emulator cough:) like WINE. So we can compare mono to be an very specific emulator only for .net applications. Even inside Windows, .NET assemblies are loaded differently than other windows dlls. The PE header contains an additional entry about the CLR version and the execution is transferred to the runtime rather than the operating system handling it. I think a similar process happens when we run the exe using mono. Here is the command.

The output of the program is displayed in the console.