C#: how to write Hello World

The “Hello, World” program is traditionally used to introduce a programming language.

Here it is in C#:

using System;
class Hello
{
static void Main() {
Console.WriteLine(“Hello, World”);
}
}

crone-park (27)

What’s the file extension?
C# source files typically have the file extension .cs.

How to compile?
You can compile the program with the Microsoft C# compiler using the command line:
csc hello.cs

What does the Using directive do?

The “Hello, World” program starts with a using directive that references the System namespace.

What does a Namespace do?
Namespaces
Organize C# programs and libraries into a hierarchy.
Contain types and other namespaces.

For example?

The System namespace contains
a number of types, such as the Console class referenced in the program, and
a number of other namespaces, such as IO and Collections.

Benefits?
A using directive that references a namespace give you unqualified use of the types that are members of that namespace.

In the Hello World example, because we have the using directive, we can use Console.WriteLine as shorthand for System.Console.WriteLine.

The Main Member
The Hello class has a single member, the Main method.

The Main method is declared with the static modifier.
Note:
Instance methods can reference a particular enclosing object instance using the keyword this.

Static methods operate without reference to a particular object.

Where’s the entry point to C# program?
The static Main method serves as the entry point to the program.

What creates the output?

The output is produced by
the WriteLine method of
the Console class in
the System namespace.

Note: C# does not have a separate runtime library. Instead, the .NET Framework is the runtime library of C#.