public class HelloWorld
public static void Main()
// This is a single line comment
/* This is a
line comment */
he 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. Line 2 above declares the class to be named ‘HelloWorld’.
Line 1 of the code declares we are using the System namespace. 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 .
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’.