C# Console WriteLine Example

C#Every programming language lesson starts with a program called Hello, World!  This program will give you a basic idea about that language.   The dotnet language makes major use of namespaces and classes.  Namespaces are basically a collection of interfaces, classes, delegates, enums, and structures.  The code for the C# dotnet program is given below, which will simply print Hello, World! in a console window.

using System;

namespace MyHelloWorld
{   
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

In the above code, we are making use of System namespace.  Because the Console class resides in the System namespace, we have to use it as our first line of code.  This is very generic way of using any namespace in a program if you want to make use of classes available in that namespace at multiple places in your code.  Otherwise, you can also write the same code as given below by using System namespace in front of Console class to print Hello, World!

public class Program
{
public static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}

WriteLine function resides in Console class and it will printing Hello, World! in a console window.   Every program has a Main function, which is also known as entry point of your program.  Without a Main function, your program will not work.   This means your program must contain Main function because this is the function where your program’s execution starts.