Very practical Mono C# Shell

Hello,
in my first tutorial I want to give you a better understanding of the very practical Mono C# shell.
Before I start, I want to give you an introduction about the Mono Framework.

Mono is an open source project which allows you to implement C# and .Net compatible solutions. It supports many platforms including consoles and mobile. The default installer comes with a lot of useful tool for the work with this framework. If you want to develop C# with Mono you can use the command line compiler and a text editor or your favorite IDE with C# support. Mono has also an IDE called Mono Develop. This is a good development environment for the start and is also available for many platforms. I prefer Visual Studio from Microsoft because it has a stronger tool set.

In my opinion C# is one of the most beautiful languages ​​and especially for beginners very suitable. The .Net Framework and associated tools make the development very easy and quick. It is also an interesting solution for larger projects. Many programs in the Windows world are based on this technology. With Mono you can break these walls depending on your intended use. There are even online games being developed with this technology.

The Mono C# Shell provides the possibility to test and run code relatively quickly. I use it a lot to test my implemented calculations or functions without checking the entire project. Another possible application is to generate files which you need to test or to process something.

Before we start, we have to install the Mono Framework. Browse to the product page and download the packages for your system. If you are using a packed management (e.g. Chocolaty) you can also use this. After downloading, install the framework. For Windows you still need to set an environment variable to your install location. For this tutorial we don’t need any IDE or text editor.

Now we are ready to start:

To start the Mono C# Shell open your terminal and type

1
csharp

Now the program should open and we can use it.
If you want to quit the application type (do not forget the semicolon at the end)

1
quit;

Lets define our first variable

1
int x = 10;

The shell supports the whole C# syntax. For some commands there is also a shortened notation. For example if you get return-value it outputs directly.
Lets try this with your variable. Now we add a value

1
x + 15;

Now the result should appear directly. Lets try it with comparing two strings without defining variables

1
"Hello" == "World!";

If you want to assign a new value to your variable do it like in C#

1
x = x / 2;

Lets define another

1
int y = 2;

We can also look at all the defined variables

1
ShowVars();

You can use your variables like any C# object

1
2
string hello = "Hello";
hello.IndexOf('o');

The next step is loading references

1
LoadPackage("gtk-sharp-2.0");

This is the equivalent of invoking the compiler with the -pkg: flag. Specifically, it integrates Mono libraries with Unix’s pkg-config. Packages allow definitions and multiple assemblies to be loaded in a single call. Let’s try an advanced exsample

1
2
3
4
5
6
7
8
9
LoadPackage ("gtk-sharp-2.0");
using Gtk;
Application.Init ();
var b = new Button ("Hello Interactive Shell");
var w = new Window ("So cute!");
b.Clicked += delegate { Application.Quit (); };
w.Add (b);
w.ShowAll ();
Application.Run ();

If you want to use a namespace, it behaves just as in C#

1
using System.IO

I hope my first tutorial helped you and I wish you much fun with experimenting.

Best,
Christoph

Leave a Reply