Friday 17 August 2018

C#: Hello World Program

I hope, you installed Visual studio. Following step-by-step procedure explains simple ‘Hello World’ application.

Step 1: Open Visual studio

Step 2: Go to File -> New -> Project.
Select “Console Application”, give the project name as “HelloWorld”.



Press OK.

It creates a file ‘Program.cs’, with some default data. It looks like below.


Update the program like below.

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.WriteLine("Welcome To C# Programming.........");
        }
    }
}

Press ‘CTRL + F5’, it opens command prompt and show the strings that we passed to 'Console.WriteLine' method.


Let’s analyze the program

‘using System’
Above statement loads a names space ‘System’. Name spaces are used to organize the code. A name space is a collection of classes, interfaces, enums, structs & delegates.

namespace HelloWorld
Above statement gives the name space ‘HelloWorld’ to this program. It is optional. You can even run the program without this statement.

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
        Console.WriteLine("Welcome To C# Programming.........");
    }
}


class Program
Class is used to encapsulate methods and properties as single entity. Ignore this for time being, I will explain about this in later posts.

static void Main(string[] args)
Main method is the entry point of your C# application. Application executions starts from here.

Console.WriteLine("Hello World");
Above statement writes the message "Hello World" to console. Console is the class resides in name space System. WriteLine is the method of class Console. 



Previous                                                 Next                                                 Home

No comments:

Post a Comment