Skip to content

Latest commit

 

History

History
341 lines (196 loc) · 14.5 KB

File metadata and controls

341 lines (196 loc) · 14.5 KB
title description ms.date ai-usage zone_pivot_groups dev_langs
Create a .NET console application
Learn how to create a .NET console application using Visual Studio, Visual Studio Code, or GitHub Codespaces.
03/03/2026
ai-assisted
code-editor-set-one
csharp
vb

Tutorial: Create a .NET console application

::: zone pivot="visualstudio"

This tutorial shows how to create and run a .NET console application in Visual Studio.

In this tutorial, you:

[!div class="checklist"]

  • Create a Visual Studio solution and console app project.
  • Create a "HelloWorld" .NET console application.
  • Enhance the app to prompt the user for their name and display it in the console window.

::: zone-end

::: zone pivot="vscode"

This tutorial shows how to create and run a .NET console application by using Visual Studio Code.

In this tutorial, you:

[!div class="checklist"]

  • Launch Visual Studio Code with a C# development environment.
  • Create a "HelloWorld" .NET console application.
  • Enhance the app to prompt the user for their name and display it in the console window.

::: zone-end

::: zone pivot="codespaces"

This tutorial shows how to create and run a .NET console application by using GitHub Codespaces.

In this tutorial, you:

[!div class="checklist"]

  • Launch a GitHub Codespace with a C# development environment.
  • Create a "HelloWorld" .NET file-based app.
  • Enhance the app to prompt the user for their name and display it in the console window.

::: zone-end

Prerequisites

::: zone pivot="visualstudio"

::: zone-end

::: zone pivot="vscode"

[!INCLUDE Prerequisites]

::: zone-end

::: zone pivot="codespaces"

::: zone-end

Create the app

::: zone pivot="visualstudio"

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

  1. Start Visual Studio.

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

    :::image type="content" source="./media/create-console-app/start-window.png" alt-text="Create a new project button selected on the Visual Studio start page":::

  3. 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 App template, and then choose Next.

    :::image type="content" source="./media/create-console-app/create-new-project.png" alt-text="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 desktop development workload installed.

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

    :::image type="content" source="./media/create-console-app/configure-new-project.png" alt-text="Configure your new project window with Project name, location, and solution name fields":::

  5. In the Additional information dialog:

    • Select .NET 10.0 (Long Term Support).
    • Select Create.

    :::image type="content" source="./media/create-console-app/additional-information.png" alt-text="Enter additional information for the console app.":::

    The template creates a simple application that displays "Hello, World!" in the console window. The code is in the Program.cs or Program.vb file:

    // See https://aka.ms/new-console-template for more information
    Console.WriteLine("Hello, World!");
    Imports System
    
    Module Program
        Sub Main(args As String())
            Console.WriteLine("Hello World!")
        End Sub
    End Module

    If the language you want to use is not shown, change the language selector at the top of the page.

    The C# template uses top-level statements to call the xref:System.Console.WriteLine(System.String)?displayProperty=nameWithType method to display a message in the console window. The Visual Basic template defines a Module Program with a Sub Main method that calls the same method.

::: zone-end

::: zone pivot="vscode"

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

  1. Start Visual Studio Code.

  2. Go to the Explorer view and select Create .NET Project. Alternatively, you can bring up the Command Palette using Ctrl+Shift+P (Command+Shift+P on MacOS) and then type ".NET" and find and select the .NET: New Project command.

    :::image type="content" source="media/create-console-app/create-dotnet-project.png" alt-text="The .NET: New Project command in the Command Palette":::

  3. After selecting the command, you need to choose the project template. Choose Console App.

  4. Select the location where you would like the new project to be created.

  5. Give your new project a name, "HelloWorld".

  6. Select .sln for the solution file format.

  7. Select Create Project.

  8. The project is created and the Program.cs file opens. You see the simple application created by the template:

    // See https://aka.ms/new-console-template for more information
    Console.WriteLine("Hello, World!");

    The code defines a class, Program, that calls the xref:System.Console.WriteLine(System.String)?displayProperty=nameWithType method to display a message in the console window.

::: zone-end

::: zone pivot="codespaces"

Open Codespaces

Start a GitHub Codespace with the tutorial environment.

  1. Open a browser window and navigate to the tutorial codespace repository.

  2. Select the green Code button, and then the Codespaces tab.

  3. Select the + sign or the green Create codespace on main button to create a new Codespace using this environment.

    :::image type="content" source="media/create-console-app/create-codespace-on-main.png" alt-text="Create a new Codespace from the tutorial repository":::

Create a .NET file-based app

In Codespaces, you'll create a file-based app. File-based apps let you build .NET applications from a single C# file without creating a traditional project file.

  1. When your codespace loads, right-click on the tutorials folder and select New File.... Enter the name HelloWorld.cs and then press Enter.

    :::image type="content" source="media/create-console-app/codespaces-create-new-file.png" alt-text="Create a new file named HelloWorld.cs in the tutorials folder":::

  2. HelloWorld.cs opens in the editor. Type or copy the following code into the file:

    :::code language="csharp" source="../snippets/get-started/csharp/hello-world.cs" id="HelloWorld":::

::: zone-end

Run the app

::: zone pivot="visualstudio"

  1. Press Ctrl+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.)

  2. Press any key to close the console window.

::: zone-end

::: zone pivot="vscode"

To run your app, select Run > Run without Debugging in the upper menu, or use the keyboard shortcut (Ctrl+F5).

If asked to select a debugger, select C# as the debugger, then select C#: Debug Active File as the Launch configuration.

The program displays "Hello, World!" and ends.

::: zone-end

::: zone pivot="codespaces"

In the terminal window, make sure the tutorials folder is the current folder, and run your program:

cd tutorials
dotnet HelloWorld.cs

The program displays "Hello, World!" and ends.

::: zone-end

Enhance the app

Enhance the application to prompt the user for their name and display it along with the date and time.

::: zone pivot="visualstudio"

  1. In Program.cs or Program.vb, replace the contents with the following code:

    :::code language="csharp" source="./snippets/create-console-app/csharp/Program.cs" id="MainMethod"::: :::code language="vb" source="./snippets/create-console-app/vb/Program.vb" id="MainMethod":::

    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 xref:System.DateTime.Now?displayProperty=nameWithType property, which contains the current local time, and assigns it to a variable named currentDate. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the xref:System.Console.Read method to wait for user input.

    xref:System.Environment.NewLine?displayProperty=nameWithType is a platform-independent and language-independent way to represent a line break. Alternatives are \n in C# and vbCrLf 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 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.

    :::image type="content" source="./media/create-console-app/hello-world-update.png" alt-text="Console window with modified program output":::

  4. Press any key to close the console window.

::: zone-end

::: zone pivot="vscode"

  1. Open Program.cs.

  2. Replace the contents of the class with the following code:

    :::code language="csharp" source="./snippets/create-console-app/csharp/Program.cs" id="MainMethod":::

    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 xref:System.DateTime.Now?displayProperty=nameWithType property, which contains the current local time, and assigns it to a variable named currentDate. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the xref:System.Console.Read method to wait for user input.

    xref:System.Environment.NewLine is a platform-independent and language-independent way to represent a line break.

    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.

  3. Save your changes.

    [!IMPORTANT] In Visual Studio Code, you have to explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.

  4. Select Run>Run without debugging.

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

    :::image type="content" source="media/create-console-app/run-program-class.png" alt-text="Terminal window with modified program output":::

    Press Enter to exit the program.

::: zone-end

::: zone pivot="codespaces"

  1. Update HelloWorld.cs with the following code:

    :::code language="csharp" source="./snippets/create-console-app/csharp/Program.cs" id="MainMethod":::

    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 xref:System.DateTime.Now?displayProperty=nameWithType property, which contains the current local time, and assigns it to a variable named currentDate. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the xref:System.Console.Read method to wait for user input.

    xref:System.Environment.NewLine is a platform-independent and language-independent way to represent a line break.

    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. Run the updated app using the following command:

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

    You'll see output similar to the following:

    What is your name? Mark
    Hello, Mark, on 1/29/2026 at 4:40 PM!
    Press Enter to exit...
    

    Press Enter to exit the program.

::: zone-end

Additional resources

::: zone pivot="visualstudio"

::: zone-end

::: zone pivot="vscode"

::: zone-end

::: zone pivot="codespaces"

Cleanup resources

GitHub automatically deletes your Codespace after 30 days of inactivity. If you plan to explore more tutorials in this series, you can leave your Codespace provisioned. If you're ready to visit the .NET site to download the .NET SDK, you can delete your Codespace. To delete your Codespace, open a browser window and navigate to your Codespaces. You see a list of your codespaces in the window. Select the three dots (...) in the entry for the learn tutorial codespace. Then select "Delete".

::: zone-end

Next steps

In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.

[!div class="nextstepaction"] Debug a .NET console application