Delen via


Hello World -- Your First Program (C# Programming Guide)

The following procedure creates a C# version of the traditional "Hello World!" program. The program displays the string Hello World!

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create and run a console application

  1. Start Visual Studio.

  2. On the File menu, point to New, and then click Project.

  3. In the Templates Categories pane, expand Visual C#, and then click Windows.

  4. In the Templates pane, click Console Application.

  5. Type a name for your project in the Name field.

  6. Click OK.

    The new project appears in Solution Explorer.

  7. If Program.cs is not open in the Code Editor, right-click Program.cs in Solution Explorer and then click View Code.

  8. Replace the contents of Program.cs with the following code.

    // 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();
            }
        }
    }
    
  9. Press F5 to run the project. A Command Prompt window appears that contains the line Hello World!

Next, the important parts of this program are examined.

Comments

The first line contains a comment. The characters // convert the rest of the line to a comment.

// A Hello World! program in C#

You can also comment out a block of text by enclosing it between the /* and */ characters. This is shown in the following example.

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

Main Method

A C# console application 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 (C# Reference) method that resides inside a class or a struct. In the previous "Hello World!" example, it resides in a class named Hello. You can declare the Main method in one of the following ways:

  • It can return void.

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

    static int Main()
    {
        //... 
        return 0;
    }
    
  • With either of the return types, it can take arguments.

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

    -or-

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

The parameter of the Main method, args, is a string array that contains the command-line arguments used to invoke the program. Unlike in C++, the array does not include the name of the executable (exe) file.

For more information about how to use command-line arguments, see the examples in Main() and Command-Line Arguments (C# Programming Guide) and How to: Create and Use C# DLLs (C# Programming Guide).

The call to ReadKey at the end of the Main method prevents the console window from closing before you have a chance to read the output when you run your program in debug mode, by pressing F5.

Input and Output

C# programs generally use the input/output services provided by the run-time library of the .NET Framework. The statement System.Console.WriteLine("Hello World!"); uses the WriteLine method. This is one of the output methods of the Console class in the run-time library. It displays its string parameter on the standard output stream followed by a new line. Other Console methods are available for different input and output operations. If you include the using System; directive at the beginning of the program, you can directly use the System classes and methods without fully qualifying them. For example, you can call Console.WriteLine instead of System.Console.Writeline:

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

For more information about input/output methods, see System.IO.

Command-Line Compilation and Execution

You can compile the "Hello World!" program by using the command line instead of the Visual Studio Integrated Development Environment (IDE).

To compile and run from a command prompt

  1. Open a Visual Studio Command Prompt window. A shortcut is available from the Start menu, under Visual Studio Tools. Alternatively, you can follow the instructions in How to: Set Environment Variables to enable command-line builds from a standard Command Prompt window.

  2. Paste the code shown in the preceding procedure into any text editor and save the file as a text file. Name the file Hello.cs. C# source code files use the extension .cs.

  3. In the Command Prompt window, navigate to the folder that contains Hello.cs.

  4. Enter the following command to compile Hello.cs. If your program has no compilation errors, an executable file that is named Hello.exe is created.

    csc Hello.cs

  5. To run the program, enter the following command:

    Hello

For more information about the C# compiler and its options, see C# Compiler Options.

See Also

Concepts

C# Programming Guide

Visual C# Samples

Main() and Command-Line Arguments (C# Programming Guide)

Reference

Inside a C# Program

Other Resources

C# Reference