Get Started with C# -II

In earlier post we talk about C# & visual studio installation. Hopefully, you have installed the visual studio on your machine. Let’s create a first console application

For making a console application in Visual Studio by using C# as the programming language, perform the following steps (These steps are for Visual Studio 2017 professional version. For other versions, these steps might differ) :

  1. Open Visual Studio.
  2. Go to File -> New -> Project.
  3. A “New Project” dialog will show up.
  4. Select “Visual C#” from the left pane of the “New Project” window and then select “Console Application” from the right pane.
  5. Give a name to your project in the “Name” Textbox shown at the bottom of the window. In our case, we will name our project “MyFirstApplication”.
  6. Select a Location, where you want your project to be stored.
  7. The last Textbox is for “Solution Name”, which by default will be the same as your project name. Leave this as it is and click “OK” to save the project.
console application first program

Now you will have a screen which will have the below depicted code:

using System;

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

        }
    }
}

Any program which is written using any of the languages available must have an entry point, i.e., in simple terms, the computer should be shown the path from where the program execution should begin. In c#, this entry point is:

static void Main(string[] args)

Code written between the squiggly brackets {}, which are suffixed to this line, is executed by the computer line by line.

So, if we have to print “Hello” upon the execution of this program, we will write the following line between the squiggly brackets as shown below:

static void Main(string[] args)
{
    Console.WriteLine("Hello");
    Console.ReadLine();
}

The complete program will look like this:

using System;

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

To run this, just hit the “Start” button on the standard toolbar, or just hit the F5 button on your keyboard.

You will now have a question that what is Console.WriteLine() and Console.ReadLine(). For now, just remember that to print anything onto the screen we use Console.WriteLine() and for reading any user input from the screen, we use Console.ReadLine().

Now you will have another question, that even though we are not taking any input from the screen, still we are using Console.ReadLine() in our program. Just remove this line and rerun your program. You will notice that even though your program ran successfully, no output was shown and there was just a flash of a black screen. That’s it. To avoid this and help us see what our program has given as output, we make use of Console.ReadLine(), so that the screen halts till the time the user does not hit the “Enter” button. Don’t worry about both these lines, we will learn more about them once we move further in our course.

At this point, I would like to bring to your notice that the computer starts the program execution one line at a time, hence we should understand what a line of code means. It means an instruction written by us for the computer and that instruction has to end somewhere. For this reason, we put a semi-colon ( ; ) at the end of the instruction.

We can have multiple instructions written on the same line, separated by semi-colons, however, as a good programming practice, we should always write only one instruction per line.

Hope this will helpful & good starting point to the novice as you have written you first program in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *