C# Tutorial Part 1 - First .NET Application

 

This is the first part of C# Tutorial and all the concepts would be explained with examples as we move forward. I would be using Visual Studio 2013 with .NET Framework 4.5 to demonstrate this tutorial.

Let us start the tutorial with a simple Console application.

So what is a Console application ? --> A console application is a program designed to be interacted through a text only computer interface like Command prompt.

So we are going to write a program using Visual studio which displays its output in the Command prompt.

Open Visual Studio, Go to File --> New --> Project .

Now you would see the below window. I have highlighted the important areas to be observed.

Image 1 

In Image 1, you will find list of installed templates on the left. Select 'Visual C#' and you will see the different types of C# applications listed. Select 'Console Application'.

The .NET Framework Version (.NET Framework 4.5) is also displayed on the top. You can select other low versions from the dropdown and develop the applications as per your requirement.

There are different text boxes at the bottom of the window which are explained below

Name : This is the project name. Please enter "HelloWorldProject"

Location : You can browse and select a desired folder on your computer where you would like to save your project / solution.

Solution name : This is the Solution name. Please enter "ConsoleApplicationDemo"

 

You would also find a checkbox "Create directory for solution" which I want you to leave it checked.

Click 'OK' after entering the Name, Location and Solution name. You will now see auto-generated code in your Visual studio. Before jumping into the code, let us try to understand Solution and Project.

What is a Project ?

A Project is a collection of source code files, resources, configuration files etc.

And What is a Solution?

A Solution is a collection of projects. One solution can contain multiple projects and every project can be of same or a different language.

The entire structure of the Application can be found in Solution Explorer to the right of the Code editor. In case you don’t see it Go to View --> Solution Explorer .

Image 2

 
 
Right click on the Solution name "ConsoleApplicationDemo" and select "Open Folder in File Explorer". This will now open the location on your computer where the solution is saved.

Image3

 
The folder contains the Solution file "ConsoleApplicationDemo.sln". This is just to show you that Solution files are saved with .SLN extension.

Double click the "HelloWorldProject" folder which you see in Image 3 where you find the project file "HelloWorldProject.csproj". All the project files of C# are saved with .CSPROJ extension. In case if you use a VB project template, the project file is saved with .VBPROJ extension. In the same path you will find a file "Program.cs" where the actual source code is written. If it’s a VB application, the code file will be named as "Program.vb". The other files and folders in the Project folder (Image 4) will be explained in later parts of the tutorial.

Image 4

 
What is the significance of "Create directory for solution" checkbox in the 'New Project' window (Image 1)?

IF CHECKED:Both the Solution and Project files are created in different folders.

IF UNCHECKED:Both the Solution and Project files are created in the same folders with same names but different extensions.

Our aim is to write a simple program which displays output in the command prompt. So add the below line to the auto-generated code in 'Program.cs' file.

Console.WriteLine("Hello World");

The above line prints all the characters between the double quotes in the Command prompt and moves the cursor to the next line. Now the programs looks as shown below

Image 5

 

To run the application go to Debug --> Start without Debugging or use a key combination of Ctrl + F5.

When you run the application you will see the output in the command prompt as shown below

Image 6

 

You have just developed your first application which prints output in the command prompt.

There is lot more to learn from your first application which will be covered in the later parts of the tutorial.

Up Next
    Ebook Download
    View all
    Learn
    View all