Hello mono and MonoDevelop

The goal is to have all development work done on Raspberry Pi. MonoDevelop as IDE, will be used to code, debug, and test C# programs. Much like Visual Studio, all tasks will be performed on GUI mode.

Mono is an open source implementation of C# and the Common Language Runtime. MonoDevelop is a cross platform IDE for C#.

In this article, I will show you how to install mono and MonoDevelop, and create your first Hello World program in C#.

mono Installation

To install mono, follow the instructions on http://www.mono-project.com/download/stable/#download-lin. Click on Linux distribution, and select the version to use.

For Raspbian, use Raspbian 9 for Stretch version. For Ubuntu Mate, you can find the version number from the name of the image file.

The mono project web site suggests that there are other packages related to mono. The packages are mono-devel, mono-dbg, referenceassemblies-pcl, ca-certificates-mono, and mono-xsp4. Please refer to the instructions page for more detail. Install the packages you need for your development work.

To check the version of mono installed, use command mono --version. The version installed, should be or later.

MonoDevelop Installation

Similar to mono, to install MonoDevelop, follow instructions on http://www.monodevelop.com/download/#fndtn-download-lin. Choose Linux distribution, and select the version to use.

After installation, use command monodevelop -? to check the version. The version installed, should be 7.5 or later.

Hello World

Now, we are ready to create the Hello World program.

Launch MonoDevelop, click menu item Programming | MonoDevelop.

To create your first program, on MonoDevelop screen, click on New under Solutions.

Let’s create a Console Project for Hello World.

You will be prompted for Project and Solution names.

Enter Project Name and Solution Name. Note the default location is /home/pi/Projects.

Click on Create button, MonoDevelop will create the skeleton program for you. And it is literally a Hello World program.

Expand the HelloWorld project on the left side of the screen. The structure is similar to projects in Visual Studio.

The source code for Program.cs is on the right side of the screen. It looks like this:

You can run the program without changing a single line of code. To run the program in debug mode, click the Run icon on upper left corner of the screen.

There are other ways to debug/run the program. Click on Run menu to see other options to run the program.

Ideally, the Hello World! message is displayed on the console. Just press any key to end debugging.

Well, it worked as expected on Unbuntu Mate. (The following screen was captured from Ubuntu Mate.)

Raspbian Only

When debugging HelloWorld program on Raspbian, you may see the following screen.

If your plan is to develop form based (Gtk#) applications or web applications, there is probably nothing to worry about. However, if you are developing Console Projects, there are two ways to fix the issue.

Use Application Output

If your program does not need to accept input from the console (e.g. Console.ReadLine()), you can redirect the output to Application Output panel. On MonoDevelop, open menu item Project | HelloWorld Options. Expand the options tree to open Run | Configurations | Default.

(Note: Solution and project options can also be changed by right-click on the solution name/project name on the Solution Explorer. Right-click on the names brings up context menu for additional functions.)

Uncheck Run on external console.

Close the Options screen. Now, if you run (debug) the program again, you will see the Hello World! message on Application Output screen.

Click on Application Output located at lower-right corner of MonoDevelop screen.

Here is the message from the program.

Install xterm

If you need to interact with console while debugging your program, you can install xterm package.

(Note: I was expecting all dependent packages should be included during the installation of MonoDevelop. So, missing a package doesn’t seem like something that should be happening. However, installing xterm did fix the problem.)

Open Terminal session. Run command sudo apt install xterm to install xterm.

Run Hello World program again. Now you can see the console messages.

Run Hello World in Production

Once you are done with program development, you are ready to send the program to production. You would probably build the program in Release mode. Programs in Release mode have better performance, but the error messages will be limited.

To build program in Release mode, switch run mode to Release.

Then open menu item Build to build the program.

The output from build operation is in the bin sub-folder under the project folder. Within the bin folder, there are Debug and Release folders. Normally, you would copy the compiled objects in either Debug or Release folder to a production folder.

To run the program, use command mono production-folder/HelloWorld.exe. If the program requires super user privilege, use command sudo mono production-folder/HelloWorld.exe.

Same command can be added to startup application, or desktop menu.

Extensions and NuGet Packages

Extensions are add-on features for MonoDevelop. Once installed, they are available for all MonoDevelop solutions. To add Extension, open menu item Tools | Extensions.

NuGet packages are intended for Projects within MonoDevelop Solution. There are several ways to add NuGet packages.

  • Select project in Solution explorer. Open menu item Project | Add NuGet Packages.
  • Right-click on project name, select from context menu Add | Add NuGet Packages.
  • Right-click on Packages, select context menu Add Packages.

When working with GPIO on Raspberry Pi, we will need to add packages with NuGet.

Version Control with Git

git is installed with Raspbian, by default. Run command git --version on Terminal, to verify if git is available.

To enable version control, run command sudo apt install monodevelop-versioncontrol on Terminal. Open MonoDevbelop, create a new project. Now you are giving options to add version control to the solution.

Leave a Reply