C# - First Program

Mukesh Kumar Posted : Views :1665

 

In this tutorial, we will learn how to create our first console application in C# using Visual Studio 2015. Here we will only create a “Hello World !” project.

Creating the project

For creating a new console application,

1.  Open Visual Studio 2012 from the start Menu

2.  After opening the visual studio, create new project from File > New > Project

           We can also use start page for New Project.

 

3.  From the New Project window, Choose “Console Application” and give it name “MyFirstApplication” and select the location for saving the project and click on OK.

4.  After clicking on OK, Visual Studio will create and open a console application within the solution explorer. By default, It has a class named with “Program.cs” and a config file “App.config

5.  We will see that our Program.cs class is auto opened. And class has minimal code for execution. This is nothing but only a template for console application. If you run this project, using the press F5, console will appear and disappear.

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Here we can see that our Program class exists inside the MyFirstApplication namespace. And Program class contains “Main” method. That is called an entry point for the class. When we run the project then Main method executes first. 

6. So, it’s time to write some code in the class for execution. Here we will add the code to write “Hello World !” on the console window.After modifying the code, code will look like below.

using System;

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

7.  Finally press F5 from the keyboard to run the application, now we will see a console window which has “Hello World !” written on it.

Console.ReadLine();

When we use  Console.ReadLine(); at the end of program then after running the program it will wait for next key that the user presses. So, It blocks the program until we does not press the next key [Enter].

Hope, you enjoy it.

 





Rate this :

Share this article






Search
Follow Us
Write for us
Here you can meet with tech professional blogs and tutorials, If you have something to share, go NExT
Find us on facebook

Followers