Home > Articles

Introducing C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Chapter 3: Introducing C#

In This Chapter

  • Simple Applications
  • Data Types
  • Escaping Characters
  • Flow Control
  • Goto
  • Loops
  • foreach
  • Documentation
  • Simple Classes
  • Namespaces—The First Contact
  • References
  • Data Types and Boxing
  • Enumerators
  • The C# Compiler
  • In Brief

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#:

using System;

class  Hello
{
    public static void Main()
    {
        Console.WriteLine("Hello World\n");
    }
}

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:

using System;

class  Hello
{
    public static void Main()
    {
        Console.Write("Hello World");
    }
}

In this case, we use Write instead of WriteLine. Also, we remove the linefeed.

[hs@localhost csharp]$ mcs hello2.cs
Compilation succeeded
[hs@localhost csharp]$ mono hello2.exe
Hello World[hs@localhost csharp]$

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:

using System;

class  Hello
{
    public static void main()
    {
        console.writeline("Hello World");
    }
}

The compiler will report an error because a part of the program is spelled the wrong way:

[hs@localhost csharp]$ mcs hello3.cs
hello3.cs(7) error CS0103: The name 'console.writeline' could not be
found in 'Hello'
Compilation failed: 1 error(s), 0 warnings

As you can see in the listing, the compiler displays a warning and an error. Many of you will have seen this performance when working with other programming languages.

  • + Share This
  • 🔖 Save To Your Account