Hello World -- Your first program

In this article, you'll use Visual Studio to create the traditional "Hello World!" program. Visual Studio is a professional Integrated Development Environment (IDE) with many features designed for .NET development. You'll use only a few of the features in Visual Studio to create this program. To learn more about Visual Studio, see Getting Started with Visual C# and Visual Basic.

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 Personalizing the IDE.

Create a new application

Start Visual Studio. You'll see the following image on Windows:

Visual Studio welcome screen on Windows

Select Create a new project in the lower right corner of the image. Visual Studio displays the New Project dialog:

Visual Studio new project screen on Windows

Note

If this is the first time you've started Visual Studio, the Recent project templates list is empty.

On the new project dialog, choose "Console App (.NET Core)" and then press Next. Give your project a name, such as "HelloWorld", then press Create.

Visual Studio opens your project. It's already a basic "Hello World!" example. Press Ctrl + F5 to run your project. Visual Studio builds your project, converting the source code into an executable. Then, it launches a command window that runs your new application. You should see the following text in the window:

Hello World!

C:\Program Files\dotnet\dotnet.exe (process 11964) exited with code 0.
Press any key to close this window . . .

Press a key to close the window.

Elements of a C# program

Let's examine the important parts of this program. 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. */

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 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. That means your program doesn't return a value.
static void Main()
{
    //...
}
  • It can also return an integer. The integer is the exit code for your application.
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.

For more information about how to use command-line arguments, see the examples in Main() and Command-Line Arguments.

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.

See also