Ever since I began using Linux as my primary OS, I have had nothing but great time. Being a .NET person in the first place, the Mono-Project is a god send for me. I tried using MonoDevelop, the IDE for Mono, on a daily basis, but learning that WCF is still largely unsupported made me drop the idea.
Lately I found myself wanting to continue my .NET practice while I am on my laptop. So I decided to try Mono once again. Instead of going for the IDE, this time I decided to use something lighter. I found two choices that suited my needs:
Geany for lightweight development and
CSharpRepl for interactive evaluation.
Let us take a quick look at these two
Geany
Fire up Geany and type the following code in a new file "hello.cs":
class Hello {
static void Main() {
System.Console.WriteLine("Hello from Mono!");
}
}
To compile this, press F8. This will bring up the Compiler message window in Geany with the following message:
gmcs /t:winexe "hello.cs" (in directory: /home/minato/Projects/Practice/mono)
Compilation finished successfully.
To execute it, press F5. A terminal windows opens up with the output of the program:
gmcs is the compiler which implements the complete C# 3.0 specification. The file hello.cs can also be compiled from the command line:
$ gmcs hello.cs
$ ls
hello.cs hello.exe
To run the file, you can't simply type in hello.exe at the prompt like in Windows. An error will be thrown, if attempted:
$ hello.exe
bash: hello.exe: command not found
The file should be treated as a Linux executable and be preceded with ./, just like how shell scripts would be run:
$ ./hello.exe
Hello from Mono!
The more appropriate way to run the executable would be using the mono command:
$ mono hello.exe
Hello from Mono!
If a truly standalone Linux executable is desired, you can use a mono utility mkbundle to generate:
$ mkbundle -o hello hello.exe --deps
$ ls
hello hello.cs hello.exe
Now you can run it like a Linux executable:
$ ./hello
Hello from Mono!
In this way we can embed programs written in Mono in other programs and shell scripts.
CsharpRepl
In Windows, to test simple pieces of C# code, there are tools like SnippetCompiler and LinqPad. Snippet Compiler is like tiny IDE, which still requires some actions be performed manually. LinqPad is closer to a REPL. You can either evaluate individual statements or a group of statements or entire class files. It is better, but still not as powerful as real REPL.
Luckily Mono provides a built in REPL called CsharpRepl. Wanting to test a snippet of C# code, I decided to give it a shot. It is available in the mono-tools-gui package. There is something similar from Microsoft called Roslyn, but I am not sure how complete it is at the moment and how much of a true REPL it is.
This is what a REPL can do. REPL stands for Read-Eval-Print-Loop. In simple terms REPL is an interactive environment used to evaluate arbitrary snippets of code. Python, Ruby and several functional languages provide a REPL out of the box. Without a REPL, testing smaller pieces of code is difficult.
Thanks to the Mono-Project, I am able to reduce the dependency on my Windows machine for simple tasks.