Loading...

First C# Project

First console app in C# with Visual Studio 2022: usage of WriteLine, ReadKey, and ReadLine.

Visual Studio 2022 is one of Microsoft’s most powerful and comprehensive IDEs. With it, you can develop C#, .NET, ASP.NET Core, WPF, WinForms, Blazor, Xamarin/.NET MAUI, Unity game projects, and many other types of applications. The project creation wizard guides users step by step in choosing the right project type. As a first step, the most suitable choice to learn the fundamentals of programming is creating a Console Project.


Creating a New Console Project

Open Visual Studio 2022 and select FileNewProject from the menu.

Creating a New Console Project

In the window that appears, type Console App in the search box and select the Console App (.NET 6/7/8) template.

Creating a New Console Project

Set the project name to FirstApp and click Create.

Creating a New Console Project

When creating the project, selecting .NET 8.0 (Long Term Support) allows you to benefit from the latest version of .NET with long-term update support.


Program.cs File

When the project is created, Visual Studio generates a Program.cs file for you. Its contents look like this:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("My first console application!");
    }
}

Note: In recent .NET versions, the Program.cs file can use top-level statements. In this example, the traditional Main method is used to clearly demonstrate the program structure.


Running the Program

From the menu, select DebugStart Without Debugging (or press Ctrl + F5). In the console window, you will see the following output:

My first console application!

The program will remain open until you press a key.


Using ReadLine

If you use Console.ReadLine(), the program will wait for you to enter some text. When the user types something and presses Enter, that text is returned as a string.


Console.Write("Enter your name: ");
string name = Console.ReadLine();

Console.Write("Enter your age: ");
string age = Console.ReadLine();

Console.WriteLine($"Hello {name}, you are {age} years old.");

Summary

At this stage, you have written your first console application with Visual Studio 2022. You learned the basics of console interaction with WriteLine, ReadKey, and ReadLine. Next, you will move on to variables and data types.