Tutorial: Create a .NET console application using Visual Studio
This tutorial shows how to create and run a .NET console application in Visual Studio 2019.
Prerequisites
Visual Studio 2019 version 16.8 or a later version with the .NET Core cross-platform development workload installed. The .NET 5.0 SDK is automatically installed when you select this workload.
For more information, see Install the .NET SDK with Visual Studio.
Create the app
Create a .NET console app project named "HelloWorld".
Start Visual Studio 2019.
Select Tools > Options > Environment > Preview features, and then select Show all .NET Core templates in the New project (requires restart).
Close and reopen Visual Studio.
On the start page, choose Create a new project.
On the Create a new project page, enter console in the search box. Next, choose C# or Visual Basic from the language list, and then choose All platforms from the platform list. Choose the Console Application template, and then choose Next.
Tip
If you don't see the .NET templates, you're probably missing the required workload. Under the Not finding what you're looking for? message, choose the Install more tools and features link. The Visual Studio Installer opens. Make sure you have the .NET Core cross-platform development workload installed.
In the Configure your new project dialog, enter HelloWorld in the Project name box. Then choose Create.
In the Additional information dialog, select .NET 5.0 (Current), and then select Create.
The template creates a simple "Hello World" application. It calls the Console.WriteLine(String) method to display "Hello World!" in the console window.
The template code defines a class, Program
, with a single method, Main
, that takes a String array as an argument:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Imports System
Module Program
Sub Main(args As String())
Console.WriteLine("Hello World!")
End Sub
End Module
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 the args array.
If the language you want to use is not shown, change the language selector at the top of the page.
Run the app
Press Ctrl+F5 to run the program without debugging.
A console window opens with the text "Hello World!" printed on the screen.
Press any key to close the console window.
Enhance the app
Enhance the application to prompt the user for their name and display it along with the date and time.
In Program.cs or Program.vb, replace the contents of the
Main
method, which is the line that callsConsole.WriteLine
, with the following code:Console.WriteLine("\nWhat is your name? "); var name = Console.ReadLine(); var date = DateTime.Now; Console.WriteLine($"\nHello, {name}, on {date:d} at {date:t}!"); Console.Write("\nPress any key to exit..."); Console.ReadKey(true);
Console.WriteLine(vbCrLf + "What is your name? ") Dim name = Console.ReadLine() Dim currentDate = DateTime.Now Console.WriteLine($"{vbCrLf}Hello, {name}, on {currentDate:d} at {currentDate:t}") Console.Write(vbCrLf + "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 the Enter key. It stores this string in a variable named
name
. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable nameddate
(currentDate
in Visual Basic). And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the Console.ReadKey(Boolean) method to wait for user input.The
\n
(orvbCrLf
in the Visual Basic code) represents a newline character.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 as interpolated strings.Press Ctrl+F5 to run the program without debugging.
Respond to the prompt by entering a name and pressing the Enter key.
Press any key to close the console window.
Additional resources
Next steps
In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.