C# Hello World

In this section, we'll create the first C# program to output "Hello World". That's the tradition to learn a new language.

Visual Studio is a software integrated development environment (IDE) from Microsoft. The following example will show you how to create a program and run it in Visual Studio 2012.
  1. Run Visual Studio 2012 then click "File" in the menu bar and choose New -> Project.
  2. Create a new project in Visual Studio

  3. In Visual C# tab, choose "Console Application" and input the application name HelloWorld and choose a folder you want to save to.
  4. Create Console Application

  5. After click "OK" button you will see the following code has been created automatically by VS.
  6. Default C# Console Application

  7. Then add the 2 lines of code to output Hello World.
  8. C# Hello World Sample Code

  9. Click DEBUG -> Start Debugging or press F5 to run the program.
  10. Default C# Console Application

  11. You will see the output below. Hit Enter to exit the program.
  12. Hello World Result

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
    class Program
        static void Main(string[] args)
            Console.WriteLine("Hello World!");
  • Line 1: System is the namespace which is provided by .Net Framework. This line means we may use the classes or other stuff inside System namespace in the following code.
  • Line 2-5: Same as the above.
  • Line 6: Empty line and it means nothing but make the code look good.
  • Line 7: Define a namespace HelloWorld. A namespace is used to isolate your classes from the outside codes. So now we can use HelloWorld.Program to locate the class Program defined here.
  • Line 8, 17: In C#, we use a pair of curly brace to define a block of code. For example, this pair of curly brace defines all the inside content belongs to HelloWorld namespace.
  • Line 9: Define a class named "Program".
  • Line 10, 16: the stuff inside the curly braces is all the content of the class.
  • Line 11: Define a static method named "Main". A static method is a class level method and cannot be invoked by objects. "void" means the method returns nothing. "string[] args" defines a parameter which can be used in the method.
  • Line 12, 15: Define the code block of the method.
  • Line 13: This line is used to output a string "Hello World". Console is a class name in System namespace and WriteLine is a static method of the class. "Hello World!" is the parameter transferred to the method. The whole method should be System.Console.WriteLine("Hello World!"). The System here is omitted because of Line 1 using statement.
  • Line 14: This line is to wait for a key pressed so that the result in Line 13 can be shown up. The program will be stopped running until you press any key in the keyboard. This is a another static method in Console class.

Do you find there is a semicolon at the end of line 13 and 14?

It is because in C# we're using semicolon to tell the compiler the statement is over. A closing curly brace means the end of a code block so semicolon is not needed behind curly brace.

Where does the program start running?

It is the Main method defined in line 11.

What happened if I press F5 button in visual studio?

The compiler will check the code first. If there is no error, the source code will be compiled to EXE file and at last run the EXE application. If there is an error, the program will be stopped until the bug is fixed.

What happened if I change the "class" in line 9 to "CLass"?

The answer is you will get a compile error. Because the compiler does not know "CLass".

Note Note
C# is a case sensitive object-oriented language and most of the keywords are lower cased.

If you don't have Visual Studio installed, you still can compile the program in command line.

  1. You need to know the folder of .Net Framework installed in your computer. For example, it was installed in C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 on my computer.
  2. Add the folder in the environment path.
    For windows XP, you can do this by right clicking "My Computer" then choose "Property". Then choose "Advanced" and click Environment Variables button. Choose Path and click Edit button then add the folder in the path if it is not existed. Then click 3 times "OK" to save the setting.
    For windows 2007, you need to right click "Computer" and choose "properties". Then click "Advanced System Setting" and click "Environment Variables". Choose Path and add the folder, at last click "OK" button to save the setting.
  3. Open Command Prompt window by run cmd command. Then go to the HelloWorld folder where it saved your source code.
  4. Then type "csc Program.cs" and Enter key to compile the file. You will see the application Program.exe was created. Then type Program run it.
  5. csc compile in command line

Congratulations! You just finished your first C# program.