Writing your First Program in C#

Creating First C# Project in Visual Studio

Open the visual studio and either press Ctrl + Shift + N, or go to File->New-> Project.



After doing the above mentioned steps following window will pop up. Select the Console App(>net Framework). Give your project a name. I am going to call it “HelloWorld” and then click OK.



Now the project window will open. You might seem some extra lines of code written there and your namespace will be different if you have named it something else other than "HelloWorld". But don't worry, all the steps will be same.

Now as you can see in the below code snippet i have added few lines of code copy and paste them.

After you have copied the above code now we will move forward to run this code and then understand what this code means. Press Ctrl + F5 or go to the debug tab and select run without debugging in the drop-down menu. After this the following window will pop up.


Press Enter to exit this window.

Now lets move towards understanding our code. The first line
using System; is a namespace just like the one we have created our self that is "Hello World". This namespace is created by the developers of C# for us so, we don't have to write everything from the scratch. This and some other namespaces are included in project by default because they are frequently used. You may see some of them are in grey, that means you are not using them in your project. So, you can remove them, if you want to.
Now lets see what is a namespace is,  Namespaces are used to provide a context to the compiler (basically a program that converts C# into form of 1,0 as computer only understand that sequence). We can have many classes with the same name (explanation on classes is below). So, how do compiler differentiate them? Well you guessed it right, through the namespace. We will discuss namespaces in detail in further topics as it needs quite a lot of explanation.

Further down you will see a line of code, Class Program. A class like a blueprint. It defines the data and behavior of a type.

Next we have the line with Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.

The next statement is Console.WriteLine("Hello World");
This line tells the compiler to write "Hello World" on the console (the black pop up window). WriteLine is a method like our Main method and it is inside the Console class defined in the System namespace.

Now moving towards the last line Console.ReadLine();
This line tells the program to wait for the key press and prevents the console from closing down immediately. 

Few things to remember

* C# is a case sensitive language that means console and Console are interpreted as different statemets
*All statements and expression must end with a semicolon (;).

Comments

Popular Posts