In this chapter, we deal with the core component of Mono. The C# compiler is
one of the most powerful tools available. It isn't just a tool for developing
applicationsit's more. Mono itself is based on C#. This shows the
importance of C# when talking about Mono and .NET.

Let's examine C# and see how simple applications can be easily implemented.

Simple Applications

Many books start with an application called Hello World. Let's stick to
this good old tradition and let's see how Hello World can be implemented
using C#:

At the beginning of the program, we include the System namespace.
We'll need this module to display text on screen. After that we define a
class called Hello. This class contains a function called
Main. The C programmers among you will know that the main function is
the one that's called at the beginning of a program. In our case, the
program does nothing other than display a simple piece of text. This example
uses the Console object and the WriteLine method.

After implementing the application, we have to compile it with the help of
the C# compiler. The next listing shows how this works:

[hs@localhost csharp]$ mcs hello.cs
Compilation succeeded

mcs is called to compile hello.cs. The output of this
process is an EXE file. To execute the program, you can use mono:

[hs@localhost csharp]$ mono hello.exe
Hello World

As you can see, you just have to call mono. mono is the
just-in-time (JIT) compiler that translates our binary to native code.
Alternatively, we could use mint to start the program:

[hs@localhost csharp]$ mint hello.exe
Hello World

In contrast to mono, mint is an interpreter that evaluates
ECMA-CLI code at runtime. Having both programs is a big advantage because
mint can be used to debug the C# compiler.

After that first example, it's time to have a look at a slightly
modified version of the software:

The output is now two lines longer. If you're planning to write
applications that are platform independent, we recommend using
WriteLine instead of Write to display linefeeds because the
Unix way of starting a new line is platform specific. Beginning a new line is
different on every system, so it's better to let C# do the job for you in
order to achieve a high abstraction.

An important point to be made about C# is that it's case sensitive,
which means that it makes a significant difference whether or not a word is
spelled in capital letters. Let's have a look at the next example: