Introduction
Actually, I was willing to write a book on this topic, “Programming C# on Ubuntu!” but I think for the overzealous crowd, I may just write a small blog post for the upcoming book of mine! C# has been one of the favorite languages of mine ever since I started programming and Ubuntu is another interesting software piece that I love! I did write a post talking about cross-platform programming using C# and it covered most of the things a C# programmer should know when it comes to writing a project that needs to be executed on multiple platforms and frameworks. In this post, I will talk about a few of the things that developers usually require when they are building applications in C#. Many things, like,
1. Syntax highlighting
I don’t doubt that high-level programmers and their efficiency is related to the syntax highlighting and the color of their code.
2. Compilers and builders
Usually, Ubuntu or Linux environments don’t come shipped with a “full-featured” IDE like Visual Studio, that most C# programmers are fond of. I will talk about the “full-featured” IDE, which has everything.
3. Debuggers
You don’t want to publish the buggy code to the public now, do you?
4. Rest of the stuff in C#
Although this is just a short snippet of “content” that I actually want to write, it will still have enough juice for you to get started programming applications in C#, in no time. But first things first, did you go and read my article about cross-platform programming in C#? If no, please go and read it at, Using C# for cross-platform development. In that post I talked about C# itself and how C# would be intuitively executed on the Ubuntu environment. In this post I will talk about the IDE, features and services and then I will talk about a few core concepts of programming using C# or managed environment. In a managed environment, most of the hard work is left to the IDE and developers are left to “just” think about the logic. That is what I am going to talk about in this blog.
Settings up the Environment
I did talk about the environment setup in the previous post, so anyways just to continue from there. The process is a bunch of small code blocks and snippets to be executed in the shell (terminal in Ubuntu terms). What we are going to install, is Mono Project’s IDE and the package includes everything that we may need to develop a C# program for Ubuntu.
Following sections will contain the resources and help tips for C# developers who want to port their applications on Ubuntu. Currently, just install the Mono Project and… Remember to install the Mono Project’s IDE, they are two different things.
First steps first, open up the terminal in your Ubuntu, (it may be different in your Ubuntu as Ubuntu itself has multiple flavors and distros).
Figure 1. Terminal emulator in All applications dropdown.
I am using Ubuntu Studio so it looked something like that, yours may be on the left-side or at the bottom or whatever! Open it up and enter the following commands to install and setup the environment for C# programming on Ubuntu.
The first step would be to add the repository to your Ubuntu system, because initially Ubuntu doesn’t know about the location to capture the Mono Project from. So you need to set up the repositories first.
# Add the GPG key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
# Add the Mono repository to the package sources
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
# Update the package list
sudo apt-get update
Add your password to the stream and continue. This would add the repository to your system, plus it would check up for the repository packages for any updates (won’t install!) This is just the start…
Got a different Linux?
Mono Project is available on Linux (of course, that is why we are using it on Ubuntu!) and that is why any Linux distro can use it. If you have a different Linux distro you may just want to visit the attached link (below), because different Linux distros would definitely have a different flavor of syntax for their terminals and shells.Install Mono on Linux
Install the Mono Project itself. Mono Project is a collection of C# compilers, Mono packages, dependencies and other tools, like command-line instructions and debuggers. Mono Project has a very handy command to install, entire set of components and binaries on the system. Just execute the following apt command.
sudo apt-get install mono-complete
This install the Mono Project and the required components, not just the compilers and “teeny tiny” stuff around. At this step, you have Mono installed, but only through command-line and terminal. If you want to make use of a full-featured IDE, continue to the next command. It is also recommended to use Mono Project’s IDE, as it would be much simpler and handy while using the IDE instead of the command-line.
sudo apt-get install monodevelop
This would take a (very long; depending on your machine and internet connection) while. Once this finishes, you can start the IDE and start using it for C# programming. To run the IDE, you can either select the symbol from the list of Applications installed
Figure 2. MonoDevelop and Mono Documentation in the list of All Applications under
Development category on Ubuntu Studio
These are two different things! Let me talk about these two things separately.
- MonoDevelop: MonoDevelop is the IDE (Integrated development environment) used to build, edit and deploy the C# applications. It is what we are usually going to use here.
- Mono Documentation: Ever stumbled upon a hard task? Ever visited MSDN for help? This is the MSDN in Mono edition here. It contains the documentation about the assemblies, languages (specifically C# language) and other required information. You would usually look in this to get help. Coming back to the thread itself… You can also start the IDE from the command-line, to start the IDE just execute the following command in your terminal
MonoDevelop
Remember? This is the IDE for Mono! The IDE would look like this
Figure 3. MonoDevelop IDE active on Ubuntu Studio.
This is it for this section, read the rest of the post for the discussions about different features of this IDE for C# programming. In many ways, MonoDevelop is similar to Visual Studio, but wait up guys, Visual Studio is Visual Studio! MonoDevelop has successfully bridged the cross-platform development using C#, I would give them this. Their environment is also great, and the performance this has is also amazing. You would love to use MonoDevelop on Ubuntu, if you are a C# fan.
Discovering the MonoDevelop
MonoDevelop is an IDE containing many tricks up its sleeves used regularly by developers ranging from small tools like text-editors, to very powerful tools like profilers and debuggers. All of these tools are used by C# developers to fine-tune their programs. In this section, I am going to demonstrate the use of MonoDevelop for C# programming on Linux environment, using Ubuntu Studio distro. This is not about programming, I will cover that in a later post, instead in this post I will cover the typical software features available for C# developers in this package.
Learning the interface is the first step toward any essential programming environment. If you are unaware of the tool that you are using or the services that you are going to get while using the tool, chances are that you are not going to get that much efficiency while programming in that particular tool. And this is the very common factor that makes (or forces) the companies to select a particular tool over another and so on. C# programmers are primarily focused on Visual Studio (and personally, I also enjoy using Visual Studio only because that is one of the greatest IDEs out there). C# programming is however not bounded to the Windows itself, since it can be programmed on multiple platforms then it should be also possible for the developers to be able to use the tools there. C# was not officially declared to support cross-platform support and in those days, Mono Project stood out and started to compile the C# programs based on ECMA standards for the Linux environments and then on the Mac OS itself. This led developers to write their C# programs, which would “surely” run on multiple platforms, before C# this would be a job to Java programs, whose bytecode would be transferred in the Jars to multiple platforms and then it would be executed on those platforms using the Java’s virtual machine support on those platforms. Roughly speaking, all of this was just the ideology that led the Mono Project to be successful, because at least in my opinion, C# is much better programming language and the .NET framework is much more extensible, flexible and portable framework to be programmed for as compared to Java programming language or the core Java APIs.
The main page of Mono Project says
Figure 4. Mono Project’s home page.
Now, counting the few of the many features that Mono Project would provide you with.
Creating a new project
Creating a new project in MonoDevelop is a very smooth and easy process. You just need to select a template, enter a few details and you are done! The main page for creating a new project in MonoDevelop is something like.
Figure 5. C# project templates.
Yes, you have seen it. MonoDevelop also supports ASP.NET web application development and I will cover that in a separate post, in near future. Just for the sake of a preview, have a look at the following templates, that are provided
Figure 6. ASP.NET application templates.
MonoDevelop also supports C and C++ programming, but that is not the discussion right now. For this blog, we will simply select C# and a Console application as the template. We simply need to add a few details like
- Name of the project.
- Location for the project.
Figure 7. Configuring the project.
Then we’re done! We will then get our project open, and we can continue exploring the rest of the stuff there.
Solution explorer
The first and foremost thing that a developer would stumble upon is the solution explorer. Solution explorer typically contains the project files, resources and other references and properties for the project that you are currently working on. The solution explorer in MonoDevelop is similar to what we have in Visual Studio in many ways…
Figure 8. Solution explorer in MonoDevelop.
This resembles Visual Studio in many ways, we have a solution name, a project name. Not just that, we are provided with
- References: Used to manage the references in our project. Can be used to add new references and assemblies to be called using the “using” directive.
- Properties: Used to configure the properties about the project, a few compilation and build-time editions and so on.
- Program.cs: This is our main program in the project.
So far, we have just viewed it. What about the options? The options are also (very much) similar to what we have in Visual Studio. We can add new items, manage the tools, manage extensions and much more all in the IDE itself.
Figure 9. Menu in the project context of the MonoDevelop IDE.
I am not going to talk about the new files, but I will definitely talk about NuGet package management, but in coming sections later. We can use these options provided here to perform many actions, like building the project, cleaning it or to add more files and resources to the project.
Running the project
Running the project can be categorized in two different sections
- Debugging
- Releasing
MonoDevelop has tools for both of these settings, you can debug your application as well as run the application in release mode. In debug mode, a debugger is attached to the process for catching the exception and to set breakpoints in the applications.
Figure 10. Debug and Release mode for executing the program.
You can select either one of these configuration and then run the application by pressing F5. Although, I had to talk about the properties and configurations of the project a bit later, but anyways you can change the configurations, add them, remove them from the project by opening the properties and then altering the settings there
Figure 11. Editing the configurations for the project.
You can then run the project in that mode, typically you can edit the CPU architecture that your program would run on; x86 or x64 editions.
Managing the references
One of the most important, most used and most critical parts of C# programming is the references to the assemblies. Without most assemblies, your program won’t run, simply! Managing the references is a very easy task in Visual Studio as well as in MonoDevelop. Initially, our program just includes the reference to the System assembly
Figure 12. Assembly reference window in MonoDevelop.
On the left side, a list of the assemblies from project or from packages or from .NET are available. While on the right side, currently selected assemblies are shown. If we include more references, more items would be shown in this list. So, let us include the famousNewtonsoft.Json assembly and see what happens.
Figure 13. Selecting a new reference from the references window.
A new reference can be seen in the right side and we have just selected the assembly reference in the left side. Once you click select, it will be added to your project.
Figure 14. Newtonsoft.Json assembly reference added to the project.
Pretty easy, though. You can include more referencs from the projects too. That is not it, if you pay attention, you can see that you add Web references too, you can manage NuGet packages too, all in the IDE itself!
Figure 15. Options to add references to your project.
You can use them to manage the references in your project.
Configuring the IDE and Project settings
Finally, I would like to talk about the IDE configurations, compiler settings and a few other editor settings that you may be interested in, just to make sure you feel at home! By default the look and the feel of the IDE won’t be suitable, and if you want to edit the look and feel of the text, you can “surely” change it.
You can manage the compile time settings, you can also sign the assembly in these settings. The settings are something like the following
Figure 16. Project options window.
The settings are categorized further. You can manage and edit the settings under each category.
- Build: Holds the configuration for the project. Like running unsafe code. Holds the configurations for running the project. Holds the tools to sign the assembly digitally.
- Source code: You can alter the look and the feel of the code editor. Allow to perform intelligent code editing features.
You can also manage many other things like the location of the project, where does the compiled binaries get places on the machine. You will find most of the settings right here. Then you can edit them and manage your project to run in multiple scenarios.
Points of Interest
This is one post in the series of articles for C# programming on Ubuntu operating system. This post covers the usual scenarios, tools and features that C# programmers typically look for in an IDE. The post doesn’t cover much about programming, but tools used in programming.
This was just the introduction post for C# programmers who want to start up programming C# on Ubuntu operating system. In upcoming posts, I will talk about
- C# language “quickies”
- C# project management and deployment.
- NuGet package management.
- Tips for refining the code and so on.
Stay tuned, there is a lot more to come… I will also publish a book, “Programming C# on Ubuntu” and, well, that’s it for now.