This is meant to be an introduction to C# and Mono. First, we need to download the mono runtime and compiler:

sudo apt-get install monodevelop mono-devel

For the purpose of this post, we are just going to create a simple program which prints out the text “Hello World”. Go ahead and make a directory for our project so we don’t make our home directory entirely too messy. I just make a directory called ‘mono’ under /home/sharms/mono for the duration of this post.

Go ahead and create a file called ‘helloWorld.cs’, and paste the following:

// Declare which namespace we want to use.
// This allows us to use Console.WriteLine
// instead of System.Console.WriteLine
// Basically like a python 'import' statement, or a
// php 'include' statement
using System;

// We create a class to contain it
class HelloWorld
{
    // Must have a main function as this is what is
    // first called when executing this
    static void Main()
    {
         // C# requires ; after statements
         // Actually output "Hello World"
        Console.WriteLine("Hello World");
    }
}

To compile it, run:

gmcs helloWorld.cs

This will generate a file called ‘helloWorld.exe’. To run it, type:

mono helloWorld.exe
Hello World

And there you have it. I also stumbled on a Makefile tutorial today, so we can make a simple make file (I won’t describe all of this, just look at how it’s used). The tutorial is at: http://www.wlug.org.nz/MakefileHowto

[sharms@sparrow mono]$ cat Makefile
COMPILER=gmcs

all: helloWorld.exe

helloWorld.exe: helloWorld.cs
	$(COMPILER) helloWorld.cs

clean:
	rm -f helloWorld.exe

.PHONY: all clean
[sharms@sparrow mono]$ make
gmcs helloWorld.cs
[sharms@sparrow mono]$ make clean
rm -f helloWorld.exe

Related posts:

  1. Annoying people with code: A gentle introduction to C# and Mono Part 2 – Data Types In part one of this series we covered making a...
  2. Annoying people with code: A gentle introduction to C# and Mono Part 3: Creating a GUI (Graphical) Mono / C# Program In this post we will cover how to make a...
  3. NCPFS NCPFS each release generally is sent out completely not working...
  4. Finding the difference between two files Overview of diff, colordiff, and meld...
  5. Quick tip: Counting lines of code I wanted to count the lines of code on a...