Sample C# Program

Sample C# Program

In deference to Martin Richards, the founder of the BCPL language and author of the first "Hello, World!" program, I present a "Hello, World!" program. Actually, I offer an enhanced version that displays "Hello, World" in English, Italian, or Spanish. The program is a console application. For simplicity, no error checking is performed. C# is case sensitive.

Here is my version of the HelloWorld program:

using System;

namespace HelloNamespace {

    class Greetings{
    public static void DisplayEnglish() {
            Console.WriteLine("Hello, world!");
    public static void DisplayItalian() {
             Console.WriteLine("Ciao, mondo!");
    public static void DisplaySpanish() {
             Console.WriteLine("Hola, imundo!");

    delegate void delGreeting();

    class HelloWorld {
        static void Main(string [] args) {

    int iChoice=int.Parse(args[0]);
    delGreeting [] arrayofGreetings={
             new delGreeting(Greetings.DisplayEnglish),
             new delGreeting(Greetings.DisplayItalian),
             new delGreeting(Greetings.DisplaySpanish)};


Csc.exe is the C# compiler. The following csc command compiles the hello.cs source file to create the executable hello.exe, which is a .NET assembly:

csc hello.cs

As a .NET assembly, the Hello.exe contains metadata and MISL code but not native binary.

Run the Hello application from the command line. Enter the program name and the language (1 for English, 2 for Italian, or 3 for Spanish). For example, the following command line displays "Ciao, mondo" ("Hello, world" in Italian).

Hello 2

The source code of the Hello application has the features common to most .NET applications: a using statement, a namespace, types, access modifiers, methods, and data.

The HelloNamespace namespace contains the Greetings and HelloWorld types. The Greetings class has three static methods, and each method displays "Hello, World" in a different language. Static methods are invoked on the type (classname.member), not an instance of that type. The static methods of the Greetings type are also public and therefore visible inside and outside the class.

Delegates define a type of function pointer. The delGreeting delegate is a container for functions pointers that point to functions that return void and have no parameters. This is not so coincidentally the function signature of the methods in the Greetings type.

The entry point of this and any other C# application is the Main method. Command-line parameters are passed as the args parameter, which is a string array. In the Hello program, the first element of the args array is a number indicating the language of choice, as inputted by the user. The Hello application converts that element to an integer. Next, the program defines an array of function pointers, which is initialized with function pointers to methods of the Greetings class. This statement invokes a function pointer to display the chosen Hello message:


The variable iChoice is an index into the delegate array. It is decremented to account for the fact that arrays in C# are zero-based.

The remainder of the chapter discusses the important features of the Hello and any other C# application, except for types that will be reviewed in the next chapter.