NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

"Hello World" in C#

Here is how "Hello World" looks in C#:

Listing 2. "Hello World" in C# (HelloCS.cs)

// Allow easy reference System namespace classes
using System;

// This "class" exists only to house entry-point
class MainApp {
    // Static method "Main" is application's entry point
    public static void Main() {
        // Write text to the console
        Console.WriteLine("Hello World using C#!");
    }
}

This code is a little longer than the equivalent for managed C++. The syntax for accessing the core library is new, where we specify the namespace rather than the name of the file in which it is found:

using System;

The most striking difference is the class specification:

class MainApp {…}

In C#, all code must be contained in methods of a class. So, to house our entry point code, we must first create a class (the name doesn't matter here). Next, we specify the entry point itself:

void Main () {…}

The compiler requires this to be called Main. The entry point must also be marked with both public and static. Also, as with the managed C++ example, our entry point takes no arguments and doesn't return anything (though different signatures for more sophisticated programs are certainly possible). The next line is:

Console.WriteLine("Hello World using C#!");    

Again, this line outputs a string using the runtime Console type. In C# however, we're able to use period to indicate scope and we don't have to place an L before the string (in C#, all strings are Unicode).

The file build.bat contains the single line necessary to build this program:

csc helloCS.cs

In this admittedly simple case, we don't have to specify anything other than the file to compile. In particular, C# doesn't use the additional link step required by C++:

C:\…\HelloWorld\cs>build
C:\…\HelloWorld\cs>csc hellocs.cs
Microsoft (R) C# Compiler Version …[NGWS runtime version…] 
Copyright (C) Microsoft Corp 2000. All rights reserved.

The default output of the C# compiler is an executable of the same name, and running this program generates the following output:

C:\…\HelloWorld\cs>hellocs
Hello World using C#!