Mono Basics

From Mono

After you get Mono installed, it's probably a good idea to run a quick Hello World program to make sure everything is setup properly. This allows you to know that your Mono is working before you try writing or running a more complex application.

Table of contents

Console Hello World

To test the most basic functionality available, copy the following code into a file called hello.cs.

using System;
 
public class HelloWorld
{
	static public void Main ()
	{
		Console.WriteLine ("Hello Mono World");
	}
 
}

To compile, use mcs:

mcs hello.cs

Using mcs to compile produces a .Net 1.1 assembly. To create a .Net 2.0 assembly, use gmcs:

gmcs hello.cs

Either compiler will create "hello.exe", which you can run using:

mono hello.exe

The program should run and output:

Hello Mono World

Gtk# Hello World

The following program tests writing a Gtk# application.

using Gtk;
using System;
 
class Hello {
 
        static void Main()
        {
                Application.Init ();
 
                Window window = new Window ("helloworld");
                window.Show();
    
                Application.Run ();
    
        }
}

To compile, use mcs as well as the -pkg command to tell the compiler to pull in the Gtk# libraries:

mcs hello.cs -pkg:gtk-sharp-2.0

Using mcs to compile produces a .Net 1.1 assembly. To create a .Net 2.0 assembly, use gmcs:

gmcs hello.cs -pkg:gtk-sharp-2.0

Either compiler will create "hello.exe", which you can run using:

mono hello.exe

Winforms Hello World

The following program tests writing a Winforms application.

using System;
using System.Windows.Forms;
 
public class HelloWorld : Form
{
	static public void Main ()
	{
		Application.Run (new HelloWorld ());
	}
 
	public HelloWorld ()
	{
		Text = "Hello Mono World";
	}
}

To compile, use mcs as well as the -pkg command to tell the compiler to pull in the Winforms libraries:

mcs hello.cs -pkg:dotnet

Using mcs to compile produces a .Net 1.1 assembly. To create a .Net 2.0 assembly, use gmcs:

gmcs hello.cs -pkg:dotnet

Either compiler will create "hello.exe", which you can run using:

mono hello.exe

ASP.Net Hello World

Need this...