This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This tutorial shows how to create and run a .NET console application in Visual Studio 2022.
Visual Studio 2022 with the.NET desktop development workload installed. The .NET 8 SDK is automatically installed when you select this workload.
For more information, seeInstall the .NET SDK with Visual Studio.
Create a .NET console app project named "HelloWorld".
Start Visual Studio 2022.
On the start page, chooseCreate a new project.

On theCreate a new project page, enterconsole in the search box. Next, chooseC# orVisual Basic from the language list, and then chooseAll platforms from the platform list. Choose theConsole App template, and then chooseNext.

Tip
If you don't see the .NET templates, you're probably missing the required workload. Under theNot finding what you're looking for? message, choose theInstall more tools and features link. The Visual Studio Installer opens. Make sure you have the.NET desktop development workload installed.
In theConfigure your new project dialog, enterHelloWorld in theProject name box. Then chooseNext.

In theAdditional information dialog:
The template creates a simple application that displays "Hello, World!" in the console window. The code is in theProgram.cs orProgram.vb file:
namespace HelloWorld; internal class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }Imports SystemModule Program Sub Main(args As String()) Console.WriteLine("Hello World!") End SubEnd ModuleIf the language you want to use is not shown, change the language selector at the top of the page.
The code defines a class,Program, with a single method,Main, that takes aString array as an argument.Main is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in theargs array.
C# has a feature namedtop-level statements that lets you omit theProgram class and theMain method. This tutorial doesn't use this feature. Whether you use it in your programs is a matter of style preference.
PressCtrl+F5 to run the program without debugging.
A console window opens with the text "Hello, World!" printed on the screen. (Or "Hello World!" without a comma in the Visual Basic project template.)

Press any key to close the console window.
Enhance the application to prompt the user for their name and display it along with the date and time.
InProgram.cs orProgram.vb, replace the contents of theMain method, which is the line that callsConsole.WriteLine, with the following code:
Console.WriteLine("What is your name?");var name = Console.ReadLine();var currentDate = DateTime.Now;Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");Console.Write($"{Environment.NewLine}Press any key to exit...");Console.ReadKey(true);Console.WriteLine("What is your name?")Dim name = Console.ReadLine()Dim currentDate = DateTime.NowConsole.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}")Console.Write($"{Environment.NewLine}Press any key to exit...")Console.ReadKey(True)This code displays a prompt in the console window and waits until the user enters a string followed by theEnter key. It stores this string in a variable namedname. It also retrieves the value of theDateTime.Now property, which contains the current local time, and assigns it to a variable namedcurrentDate. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls theConsole.ReadKey(Boolean) method to wait for user input.
Environment.NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are\n in C# andvbCrLf in Visual Basic.
The dollar sign ($) in front of a string lets you put expressions such as variable names in curly braces in the string. The expression value is inserted into the string in place of the expression. This syntax is referred to asinterpolated strings.
PressCtrl+F5 to run the program without debugging.
Respond to the prompt by entering a name and pressing theEnter key.

Press any key to close the console window.
In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?