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#.
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 220.127.116.11 or later.
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.
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.
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.)
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.