C# Hello World - Your First C# Program

The “Hello World!” program is often the first program we see when we dive into a new language. It simply prints Hello World! on the output screen.

The purpose of this program is to get us familiar with the basic syntax and requirements of a programming language.


"Hello World!" in C#

// Hello World! program
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
    }
}

When you run the program, the output will be:

Hello World!

How the "Hello World!" program in C# works?

Let's break down the program line by line.

  1. // Hello World! Program

    // indicates the beginning of a comment in C#. Comments are not executed by the C# compiler.

    They are intended for the developers to better understand a piece of code. To learn more about comments in C#, visit C# comments.
     
  2. namespace HelloWorld{...}

    The namespace keyword is used to define our own namespace. Here we are creating a namespace called HelloWorld.

    Just think of namespace as a container which consists of classes, methods and other namespaces. To get a detailed overview of namespaces, visit C# Namespaces.
     
  3. class Hello{...}

    The above statement creates a class named - Hello in C#. Since, C# is an object-oriented programming language, creating a class is mandatory for the program’s execution.
     
  4. static void Main(string[] args){...}

    Main() is a method of class Hello. The execution of every C# program starts from the Main() method. So it is mandatory for a C# program to have a Main() method.

    The signature/syntax of the Main() method is:
    static void Main(string[] args)
    {
        ...
    }
    We’ll learn more about methods in the later chapters.
     
  5. System.Console.WriteLine("Hello World!");

    For now, just remember that this is the piece of code that prints Hello World! to the output screen.You’ll learn more about how it works in the later chapters.

Alternative Hello World! implementation

Here’s an alternative way to write the “Hello World!” program.

// Hello World! program
using System;
 
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Notice in this case, we’ve written using System; at the start of the program. By using this, we can now replace

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

with

Console.WriteLine("Hello World!");

This is a convenience we’ll be using in our later chapters as well.


Things to remember from this article

  • Every C# program must have a class definition.
  • The execution of program begins from the Main() method.
  • Main() method must be inside a class definition.

This is just a simple program for introducing C# to a newbie. If you did not understand certain things in this article, that's okay (even I did not when I started). As we move on with this tutorial series, everything will start to make sense.

Did you find this article helpful?