C# Tutorial Lesson 3: Getting Started

In order to use C# and the .NET framework classes, you first need to install either the .NET framework SDK, or else Visual Studio .NET. Some useful advice about getting hold of and installing the former can be found at:

http://www.mastercsharp.com/article.aspx?ArticleID=17&TopicID=10

In the next section we run through a standard 'hello world' example, with links to lessons covering the different parts of the program.

A First C# Program: 'Hello World'

Let's begin in the traditional way, by looking at the code of a Hello World program (note that the tabulation and line numbers are included just for the sake of readability).

1.

using System;

2.

public class HelloWorld

3.

{

4.

    public static void Main()

5.

    {

6.

        // This is a single line comment

7.

        /* This is a

8.

        multiple

9.

        line comment */

10.

        Console.WriteLine("Hello World! From Softsteel Solutions");

11.

    }

12.

}


The first thing to note about C# is that it is case-sensitive. You will therefore get compiler errors if, for instance, you write 'console' rather than 'Console'.

The second thing to note is that every statement finishes with a semicolon (;) or else takes a code block within curly braces.

As C# is an object-oriented language, C# programs must be placed in classes (classes are discussed in lesson 11, but if you are new to object orientation we suggest that you first read some introductory material). Line 2 above declares the class to be named 'HelloWorld'.

Line 1 of the code declares we are using the System namespace (namespaces are also covered in lesson 11). The point of this declaration is mostly to save ourselves time typing. Because the 'Console' object used in line 10 of the code actually belongs to the 'System' namespace, its fully qualified name is 'System.Console'. However, because in line 1 we declare that the code is using the System namespace, we can then leave off the 'System.' part of its name within the code.

When compiled and run, the program above will automatically run the 'Main' method declared and begun in line 4. Note again C#'s case-sensitivity - the method is 'Main' rather than 'main'.

Lines 6-9 of the program are ignored by the compiler, being comments entered by the programmer for his own benefit. Line 6 shows a single line comment, in which everything on the line after the two forward slashes is ignored by the compiler. Lines 7-9 demonstrate a multi-line comment, in which everything between the opening /* and closing */ is ignored, even when it spans multiple lines.

The statement on line 10 calls the 'WriteLine' method of the Console class in the System namespace. It should be obvious how this works in the given example - it just prints out the given string to the 'Console' (on PC machines this will be a DOS prompt). For a more complicated use of the WriteLine method, see lesson 7.

In order to run it, the program above must first be saved in a file. Unlike in Java, the name of the class and the name of the file in which it is saved do not need to match up, although it does make things easier if you use this convention. In addition, you are free to choose any extension for the file, but it is usual to use the extension '.cs'.

Suppose that you have saved the file as 'HelloWorld.cs'. Then to compile the program from a command line, you would use the command

csc HelloWorld.cs

(for Visual Studio .NET users: compile by pressing Ctrl-Shift-B)

This command would generate the executable HelloWorld.exe, which could be run in the usual way, by entering its name:

HelloWorld

(for Visual Studio .NET users: run by pressing Ctrl-F5)

Fairly obviously, this program would produce the output:

Hello World! From Softsteel Solutions.

Link Building Information