Inside a C# Program (Visual C#)

Visual Studio 2008

In order to understand how a C# program works, let's examine the traditional "Hello World!" program, dealing with each line of C# code in turn. If you want to create your own version of this program, see the topic How to: Create a C# Console Application for a step-by-step guide to writing, building, and running a similar C# application.

The C# language uses classes to organize and package code. In fact, all executable C# code must be contained in a class, even in a short program like "Hello World!" Here is the complete program that displays "Hello World!" in the console window.

// A Hello World! program in C# 
using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            System.Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
}

When you create a console application with Visual C# Express Edition, the first lines in the code editor contain using directives listing several .NET Framework namespaces. A namespace is a way of grouping classes and structs together in a way that limits their scope and avoids name conflicts with other classes and structs. When you create a program in Visual C# Express, a namespace is automatically created for you. To use classes from other namespaces in your program, you must specify them with a using Directive. The most commonly used.NET Framework namespaces are listed by default when you create a new application. If you use classes from other namespaces in the class library, you must add a using directive for that namespace to the source file. For more information on namespaces, see Namespaces.

When the Code Editor detects that you have declared a class or struct that it cannot find in the namespaces listed in the current using directives, it will suggest namespaces that contain the class or struct.

After the using statements, the next line contains a comment. Comments are useful for including notes to yourself or other programmers.

// A Hello World! program in C#

The characters // convert the rest of the line to a comment. You can also comment a block of text by placing it between the characters /* and */, for example:

/* A "Hello World!" program in C#.
This program displays the string "Hello World!" on the screen. */

You can also use a formatting option to comment code automatically. For more information, see How to: Comment Out Lines of Code (C# Express).

The C# language uses classes to package code: all executable C# code must be contained in a class. For more information, see Classes.

  • The C# program must contain a Main method, in which control starts and ends. The Main method is where you create objects and execute other methods. The Main method is a static method that resides inside a class or a struct. In the "Hello World!" example, it resides inside the Program class.

The Main method can be defined in one of the following ways:

  • It can return void:

    static void Main()
    {
        //...
    }
    
  • It can also return an int:

    static int Main()
    {
        //... 
        return 0;
    }
    
  • It can take arguments, which are useful for command line utilities:

    static void Main(string[] args)
    {
        //...
    }
    

    -or-

    static int Main(string[] args)
    {
        //... 
        return 0;
    }
    

The parameter of the Main method is a string array that represents the command-line arguments used to invoke the program. Notice that unlike C++, this array does not include the name of the executable (.exe) file. For more information, see Main() and Command-Line Arguments (C# Programming Guide).

C# console programs generally use the input/output services provided by .NET Framework Console class. The statement Console.WriteLine("Hello, World!"); uses the WriteLine method. It displays its string parameter on the command-line window followed by a new line. Other Console methods are used for different input and output operations. The Console class is a member of the System namespace. If the using System; statement was not included at the beginning of the program, you would have to specify the System classes like this:

System.Console.WriteLine("Hello World!");

The WriteLine method is very useful, and you will use it a lot if you are writing console applications.

WriteLine can display strings:

Console.WriteLine("Hello World!");

WriteLine can also display numbers:

int x = 42;
Console.WriteLine(x);

If you need to display several items, use {0} to represent the first item, {1} the second item, and so on, like this:

int year = 1066;
string battle = "Battle of Hastings";
Console.WriteLine("The {0} took place in {1}.", battle, year);

The output will look like this:

The Battle of Hastings took place in 1066.

Community Additions

ADD
Show: