First C# Program:

In the previous section, we have created a console project. Now, let's write simple C# code to understand important building blocks.

Every console application starts from the Main() method of Program class. The following example code displays "Hello World!!" on the console.

Example: Simple Console Project with C#

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

namespace CSharpTutorials
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "Hello World!!";

            Console.WriteLine(message);
        }
    }
}

The following image illustrates the important parts of the above example.

C# Program

Explaination of above points:

  1. Every .NET application takes the reference of the necessary .NET framework namespaces that it is planning to use with the "using" keyword e.g. using System.Text
  2. Declare the namespace for the current class using the "namespace" keyword e.g. namespace CSharpTutorials.FirstProgram
  3. We then declared a class using the "class" keyword: class Program
  4. The Main() is a method of Program class which is the entry point of the console application.
  5. String is a data type.
  6. 'message' is a variable, that holds a value of a specified data type.
  7. "Hello World!!" is the value of the message variable.
  8. Console is a .NET framework class. WriteLine() is a method which you can use to display messages to the console.
Note : Every line or statement in C# must end with a semicolon (;).

Compile and Run C# Program:

In order to see the output of the above C# program, we have to compile it and run it by pressing Ctrl + F5, or clicking Run button or by clicking the "Debug" menu and clicking "Start Without Debugging". You will see following output in the console:

Hello World!!

So this is the basic code items that you will probably use in every C# code. Let's learn about C# Class in the next section.