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

Create the app

Create a .NET console app project named "HelloWorld".

  1. Start Visual Studio 2019.

  2. Select Tools > Options > Environment > Preview features, and then select Show all .NET Core templates in the New project (requires restart).

    Show all .NET templates option

  3. Close and reopen Visual Studio.

  4. On the start page, choose Create a new project.

    Create a new project button selected on the Visual Studio start page

  5. 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.

    Create a new project window with filters selected

    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.

  6. In the Configure your new project dialog, enter HelloWorld in the Project name box. Then choose Create.

    Configure your new project window with Project name, location, and solution name fields

  7. In the Additional information dialog, select .NET 5.0 (Current), and then select Create.

    Additional information dialog

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

  1. Press Ctrl+F5 to run the program without debugging.

    A console window opens with the text "Hello World!" printed on the screen.

    Console window showing Hello World Press any key to continue

  2. 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.

  1. In Program.cs or Program.vb, replace the contents of the Main method, which is the line that calls Console.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 named date (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 (or vbCrLf 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.

  2. Press Ctrl+F5 to run the program without debugging.

  3. Respond to the prompt by entering a name and pressing the Enter key.

    Console window with modified program output

  4. 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.