10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

First C# Program

Updated on Sep 3, 2025
 
45,960 Views

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 14: 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 15: 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.

Creating a project in Visual Studio

Figure 16: Creating a project 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 17: 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

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

Source Code: First program in C#

Here,

  1. Using System: The using keyword adds the namespace in the program. Here, “using System” adds the System namespace in the program.
  2. Namespace: The namespace keyword is used to declare a namespace “ConsoleApp1”. The namespace “ConsoleApp1” has the class “Program”.
  3. Class Program: It declares a class named Program
  4. Main() method: The first method that gets executed. It is the entry point of a C# program.
  5. 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 18: 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#.

+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses