top

Search

C# Tutorial

We saw in the “Set Environment” lesson, how to install Microsoft Visual Studio 2017.After installing Visual Studio successfully, now let us see how to create a new project and run first program. We will create C# Console Application project.Create a new project in Visual StudioOpen Visual Studio and go to “File” > “New” > “Project”:Figure 15: Create a new project in C#Now, “New Project” dialog box will open.From the left tab, select “Visual C#” and then select “Console App (.NET Framework)” to create a Console Application.Figure 16: Select Console App (.NET Framework) to create Console ApplicationAfter selecting, add the name of the project, set the location and click OK.Above, we have set the default name and location as:Name: ConsoleApp1Location: C:\users\amit_\source\reposChange the location, if you wish, using the “Browse” button on the right. With that, you can also change the name of the console application. After adding the name and other details, click OK to start creating the project as shown below.Figure 17: The project initiates in Visual StudioAs shown in the above screenshot, the project is being created.After some seconds, the project will be created successfully and “Program.cs” file would be visible as shown below.Figure 18: Project created successfullyAs shown above, the default code is visible. Now let us understand it and do some changes.Let us add some lines to the “Program.cs” file and display it as shown below:Program.csSource Code: First program in C#using System; namespace ConsoleApp1 {    class Example    {        static void Main(string[] args)        {            Console.WriteLine("Hello World");            Console.ReadLine();        }    } }Here,using System: The using keyword adds the namespace in the program. Here, “using System” adds the System namespace in the program.namespace: The namespace keyword is used to declare a namespace “ConsoleApp1”. The namespace “ConsoleApp1” has the class “Program”.class Program: It declares a class named ProgramMain() method: The first method that gets executed. It is the entry point of a C# program.Console.WriteLine: A method of the Console class is WriteLine. This displays the message on the screen. We added “Hello World” inside it; therefore, the same is added in it.The same code is added in the Visual Studio IDE project we created:Figure 19: Added some code in Visual Studio IDE C# Console ApplicationRun the programNow, let’s see how to run the project and display the result. Run any C# Console Application using the following two options:Press “Start” button, orPress F5Both of the above options will run the program.To find the “Start” button, refer the below screenshot:Figure 19: Click Start button to run the projectOn clicking “Start”, the program runs and the following console is visible that displays the result “Hello World”:Figure 20: Output of the Console ApplicationFollow the above steps to run your first program in C#.
logo

C# Tutorial

First C# Program

We saw in the “Set Environment” lesson, how to install Microsoft Visual Studio 2017.

After installing Visual Studio successfully, now let us see how to create a new project and run first program. We will create C# Console Application project.

Create a new project in Visual Studio

Open Visual Studio and go to “File” > “New” > “Project”:

Create a new project in C#

Figure 15: Create a new project in C#

Now, “New Project” dialog box will open.

From the left tab, select “Visual C#” and then select “Console App (.NET Framework)” to create a Console Application.

Select Console App (.NET Framework) to create Console Application

Figure 16: Select Console App (.NET Framework) to create Console Application

After selecting, add the name of the project, set the location and click OK.

Above, we have set the default name and location as:

Name: ConsoleApp1

Location: C:\users\amit_\source\repos

Change the location, if you wish, using the “Browse” button on the right. With that, you can also change the name of the console application.

 After adding the name and other details, click OK to start creating the project as shown below.

The project initiates in Visual Studio

Figure 17: The project initiates in Visual Studio

As shown in the above screenshot, the project is being created.

After some seconds, the project will be created successfully and “Program.cs” file would be visible as shown below.

Project created successfully

Figure 18: Project created successfully

As shown above, the default code is visible. Now let us understand it and do some changes.

Let us add some lines to the “Program.cs” file and display it as shown below:
Program.cs

Source Code: First program in C#

using System;
namespace ConsoleApp1
{
   class Example
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Hello World");
           Console.ReadLine();
       }
   }
}

Here,

  • using System: The using keyword adds the namespace in the program. Here, “using System” adds the System namespace in the program.
  • namespace: The namespace keyword is used to declare a namespace “ConsoleApp1”. The namespace “ConsoleApp1” has the class “Program”.
  • class Program: It declares a class named Program
  • Main() method: The first method that gets executed. It is the entry point of a C# program.
  • Console.WriteLine: A method of the Console class is WriteLine. This displays the message on the screen. We added “Hello World” inside it; therefore, the same is added in it.

The same code is added in the Visual Studio IDE project we created:

Added some code in Visual Studio IDE C# Console Application

Figure 19: Added some code in Visual Studio IDE C# Console Application

Run the program

Now, let’s see how to run the project and display the result. Run any C# Console Application using the following two options:

  1. Press “Start” button, or
  2. Press F5

Both of the above options will run the program.
To find the “Start” button, refer the below screenshot:

 Click Start button to run the project

Figure 19: Click Start button to run the project

On clicking “Start”, the program runs and the following console is visible that displays the result “Hello World”:

Output of the Console Application

Figure 20: Output of the Console Application

Follow the above steps to run your first program in C#.

Leave a Reply

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

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial