Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 95 additions & 13 deletions docs/core/tutorials/debugging-with-visual-studio-code.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
---
title: Debug a .NET console application using Visual Studio Code
description: Learn how to debug a .NET console app using Visual Studio Code.
ms.date: 10/23/2025
ms.date: 01/27/2026
zone_pivot_groups: code-editor-set-one
---
# Tutorial: Debug a .NET console application using Visual Studio Code

::: zone pivot="vscode"

This tutorial introduces the debugging tools available in Visual Studio Code for working with .NET apps.

::: zone-end

::: zone pivot="codespaces"

This tutorial introduces the debugging tools available in GitHub Codespaces for working with .NET apps.

::: zone-end

## Prerequisites

This tutorial works with the console app that you create in [Create a .NET console application using Visual Studio Code](with-visual-studio-code.md).
Expand All @@ -17,16 +28,30 @@ This tutorial works with the console app that you create in [Create a .NET conso

In the Debug configuration, a program compiles with full symbolic debug information and no optimization. Optimization complicates debugging, because the relationship between source code and generated instructions is more complex. The release configuration of a program has no symbolic debug information and is fully optimized.

::: zone pivot="vscode"

By default, Visual Studio Code launch settings use the Debug build configuration, so you don't need to change it before debugging.

1. Start Visual Studio Code.

1. Open the folder of the project that you created in [Create a .NET console application using Visual Studio Code](with-visual-studio-code.md).

::: zone-end

::: zone pivot="codespaces"

By default, GitHub Codespaces uses the Debug build configuration, so you don't need to change it before debugging.

1. Open your GitHub Codespace that you created in [Create a .NET console application using Visual Studio Code](with-visual-studio-code.md).

::: zone-end

## Set a breakpoint

A *breakpoint* temporarily interrupts the execution of the application before the line with the breakpoint is run.

::: zone pivot="vscode"

1. Open the *Program.cs* file.

1. Set a *breakpoint* on the line that displays the name, date, and time, by clicking in the left margin of the code window. The left margin is to the left of the line numbers. Other ways to set a breakpoint are by pressing <kbd>F9</kbd> or choosing **Run** > **Toggle Breakpoint** from the menu while the line of code is selected.
Expand All @@ -35,8 +60,22 @@ A *breakpoint* temporarily interrupts the execution of the application before th

:::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-set-net6.png" alt-text="Breakpoint set":::

::: zone-end

::: zone pivot="codespaces"

1. Open the *HelloWorld.cs* file.

1. Set a *breakpoint* on the line that displays the name, date, and time, by clicking in the left margin of the code window. The left margin is to the left of the line numbers.

:::image type="content" source="media/debugging-with-visual-studio-code/codespaces-breakpoint-set.png" alt-text="Breakpoint set":::

::: zone-end

## Start debugging

::: zone pivot="vscode"

1. Open the Debug view by selecting the Debugging icon on the left side menu.

:::image type="content" source="media/debugging-with-visual-studio-code/select-debug-pane-net6.png" alt-text="Open the Debug tab in Visual Studio Code":::
Expand All @@ -57,6 +96,26 @@ A *breakpoint* temporarily interrupts the execution of the application before th

:::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-hit-net6.png" alt-text="Breakpoint hit, showing Locals":::

::: zone-end

::: zone pivot="codespaces"

1. Open the Debug view by selecting the Debugging icon on the left side menu.

:::image type="content" source="media/debugging-with-visual-studio-code/codespaces-select-debug-pane.png" alt-text="Open the Debug tab in Visual Studio Code":::

1. Select **Run and Debug**. If asked, select **C#** as the debugger and then select **C#: Debug Active File** as the Launch Configuration.

1. Select the **Debug Console** tab to see the "What is your name?" prompt that the program displays before waiting for a response.

1. Enter a string in the **Debug Console** window in response to the prompt for a name, and then press <kbd>Enter</kbd>.

Program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method runs. The **Locals** section of the **Variables** window displays the values of variables that are defined in the currently running method.

:::image type="content" source="media/debugging-with-visual-studio-code/codespaces-breakpoint-hit.png" alt-text="Breakpoint hit, showing Locals":::

::: zone-end

## Use the Debug Console

The **Debug Console** window lets you interact with the application you're debugging. You can change the value of variables to see how it affects your program.
Expand All @@ -77,7 +136,7 @@ The **Debug Console** window lets you interact with the application you're debug

The values displayed in the console window correspond to the changes you made in the **Debug Console**.

:::image type="content" source="media/debugging-with-visual-studio-code/changed-variable-values.png" alt-text="Terminal showing the entered values":::
:::image type="content" source="media/debugging-with-visual-studio-code/codespaces-changed-variable-values.png" alt-text="Terminal showing the entered values":::

1. Press <kbd>Enter</kbd> to exit the application and stop debugging.

Expand Down Expand Up @@ -127,45 +186,43 @@ The program displays the string that the user enters. What happens if the user d

Visual Studio Code also allows you to step line by line through a program and monitor its execution. Ordinarily, you'd set a breakpoint and follow program flow through a small part of your program code. Since this program is small, you can step through the entire program.

1. Set a breakpoint on the opening curly brace of the `Main` method.
1. Set a breakpoint on the line of code that displays the "What is your name?" prompt.

1. Press <kbd>F5</kbd> to start debugging.

Visual Studio Code highlights the breakpoint line.

At this point, the **Variables** window shows that the `args` array is empty, and `name` and `currentDate` have default values.

1. Select **Run** > **Step Into** or press <kbd>F11</kbd>.
1. Select **Step Into** from the Debug toolbar or press <kbd>F11</kbd>.

:::image type="content" source="media/debugging-with-visual-studio-code/step-into.png" alt-text="Step-Into button":::

Visual Studio Code highlights the next line.

1. Select **Run** > **Step Into** or press <kbd>F11</kbd>.
1. Visual Studio Code runs the `Console.WriteLine` for the name prompt and highlights the next line of execution. The next line is the `Console.ReadLine` for the `name`. The **Variables** window is unchanged, and the **Terminal** tab shows the "What is your name?" prompt.

Visual Studio Code runs the `Console.WriteLine` for the name prompt and highlights the next line of execution. The next line is the `Console.ReadLine` for the `name`. The **Variables** window is unchanged, and the **Terminal** tab shows the "What is your name?" prompt.
1. Select **Step Into** or press <kbd>F11</kbd>.

1. Select **Run** > **Step Into** or press <kbd>F11</kbd>.

Visual Studio highlights the `name` variable assignment. The **Variables** window shows that `name` is still `null`.
Visual Studio Code highlights the `name` variable assignment. The **Variables** window shows that `name` is still `null`.

1. Respond to the prompt by entering a string in the Terminal tab and pressing <kbd>Enter</kbd>.

The **Debug Console** tab might not display the string you enter while you're entering it, but the <xref:System.Console.ReadLine%2A?displayProperty=nameWithType> method will capture your input.

1. Select **Run** > **Step Into** or press <kbd>F11</kbd>.
1. Select **Step Into** or press <kbd>F11</kbd>.

Visual Studio Code highlights the `currentDate` variable assignment. The **Variables** window shows the value returned by the call to the <xref:System.Console.ReadLine%2A?displayProperty=nameWithType> method. The **Terminal** tab displays the string you entered at the prompt.

1. Select **Run** > **Step Into** or press <kbd>F11</kbd>.
1. Select **Step Into** or press <kbd>F11</kbd>.

The **Variables** window shows the value of the `currentDate` variable after the assignment from the <xref:System.DateTime.Now?displayProperty=nameWithType> property.

1. Select **Run** > **Step Into** or press <kbd>F11</kbd>.
1. Select **Step Into** or press <kbd>F11</kbd>.

Visual Studio Code calls the <xref:System.Console.WriteLine(System.String,System.Object,System.Object)?displayProperty=nameWithType> method. The console window displays the formatted string.

1. Select **Run** > **Step Out** or press <kbd>Shift</kbd>+<kbd>F11</kbd>.
1. Select **Step Out** or press <kbd>Shift</kbd>+<kbd>F11</kbd>.

:::image type="content" source="media/debugging-with-visual-studio-code/step-out.png" alt-text="Step-Out button":::

Expand All @@ -177,15 +234,40 @@ Visual Studio Code also allows you to step line by line through a program and mo

Once you've tested the Debug version of your application, you should also compile and test the Release version. The Release version incorporates compiler optimizations that can affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications.

::: zone pivot="vscode"

To build and test the Release version of your console application, open the **Terminal** and run the following command:

```dotnetcli
dotnet run --configuration Release
```

::: zone-end

::: zone pivot="codespaces"

To build and test the Release version of your console application, run the following command in the terminal:

```dotnetcli
dotnet run --configuration Release HelloWorld.cs
```

::: zone-end

## Additional resources

::: zone pivot="vscode"

- [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging)

::: zone-end

::: zone pivot="codespaces"

- [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging)
- [GitHub Codespaces documentation](https://docs.github.com/codespaces)

::: zone-end

## Next steps

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 111 additions & 3 deletions docs/core/tutorials/publishing-with-visual-studio-code.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
---
title: Publish a .NET console application using Visual Studio Code
description: Learn how to use Visual Studio Code and the .NET CLI to create the set of files that are needed to run a .NET application.
ms.date: 09/12/2024
ms.date: 01/28/2026
zone_pivot_groups: code-editor-set-one
---
# Tutorial: Publish a .NET console application using Visual Studio Code

This tutorial shows how to publish a console app so that other users can run it. Publishing creates the set of files that are needed to run an application. To deploy the files, copy them to the target machine.

The .NET CLI is used to publish the app, so you can follow this tutorial with a code editor other than Visual Studio Code if you prefer.
The .NET CLI is used to publish the app.

## Prerequisites

- This tutorial works with the console app that you create in [Create a .NET console application using Visual Studio Code](with-visual-studio-code.md).

## Publish the app

::: zone pivot="vscode"

1. Start Visual Studio Code.

1. Open the *HelloWorld* project folder that you created in [Create a .NET console application using Visual Studio Code](with-visual-studio-code.md).
Expand All @@ -29,6 +32,32 @@ The .NET CLI is used to publish the app, so you can follow this tutorial with a
dotnet publish
```

::: zone-end

::: zone pivot="codespaces"

1. Open your GitHub Codespace that you created in [Create a .NET console application using Visual Studio Code](with-visual-studio-code.md).

1. Add the following line of code to the top of *HelloWorld.cs*:

```csharp
#:property PublishAot=false
```

This property directive, disables native ahead-of-time (AOT) compilation and the app will use the standard just-in-time (JIT) compiler at runtime. The published output will be framework-dependent or a standard self-contained deployment (not native code).

1. In the terminal, make sure you're in the *tutorials* folder.

1. Run the following command:

```dotnetcli
dotnet publish HelloWorld.cs
```

::: zone-end

::: zone pivot="vscode"

The default build configuration is *Release*, which is appropriate for a deployed site running in production. The output from the Release build configuration has minimal symbolic debug information and is fully optimized.

The command output is similar to the following example:
Expand All @@ -42,8 +71,27 @@ The .NET CLI is used to publish the app, so you can follow this tutorial with a
HelloWorld -> C:\Projects\HelloWorld\bin\Release\net8.0\publish\
```

::: zone-end

::: zone pivot="codespaces"

The command creates an independent executable.

The command output is similar to the following example:

```output
Restore complete (0.5s)
HelloWorld net10.0 succeeded (4.0s) → artifacts\HelloWorld\

Build succeeded in 5.1s
```

::: zone-end

## Inspect the files

::: zone pivot="vscode"

By default, the publishing process creates a framework-dependent deployment, which is a type of deployment where the published application runs on a machine that has the .NET runtime installed. To run the published app you can use the executable file or run the `dotnet HelloWorld.dll` command from a command prompt.

In the following steps, you'll look at the files created by the publish process.
Expand All @@ -66,7 +114,7 @@ In the following steps, you'll look at the files created by the publish process.

- *HelloWorld.exe* (*HelloWorld* on Linux or macOS.)

This is the [framework-dependent executable](../deploying/index.md#framework-dependent-deployment) version of the application. The file is operating-system-specific.
This is the [framework-dependent executable](../deploying/index.md#framework-dependent-deployment) version of the application. The file is operating-system-specific.

- *HelloWorld.pdb* (optional for deployment)

Expand All @@ -76,8 +124,48 @@ In the following steps, you'll look at the files created by the publish process.

This is the application's runtime configuration file. It identifies the version of .NET that your application was built to run on. You can also add configuration options to it. For more information, see [.NET runtime configuration settings](../runtime-config/index.md#runtimeconfigjson).

::: zone-end

::: zone pivot="codespaces"

For a single-file application, the publishing process creates an artifacts directory with a compiled assembly file. The published application can be run using the `dotnet` command.

In the following steps, you'll look at the files created by the publish process.

1. Select the **Explorer** in the left navigation bar.

1. Expand *artifacts/HelloWorld*.

:::image type="content" source="media/publishing-with-visual-studio-code/codespaces-published-files-output.png" alt-text="Explorer showing published files":::

As the image shows, the published output includes the following files:

- *HelloWorld*

This is the [framework-dependent executable](../deploying/index.md#framework-dependent-deployment) version of the application. The file is operating-system-specific.

- *HelloWorld.deps.json*

This is the application's runtime dependencies file. It defines the .NET components and the libraries (including the dynamic link library that contains your application) needed to run the app. For more information, see [Runtime configuration files](https://github.com/dotnet/cli/blob/4af56f867f2f638b4562c3b8432d70f7b09577b3/Documentation/specs/runtime-configuration-file.md).

- *HelloWorld.dll*

This is the [framework-dependent deployment](../deploying/index.md#cross-platform-dll-deployment) version of the application. To run this dynamic link library, enter `dotnet HelloWorld.dll` at a command prompt. This method of running the app works on any platform that has the .NET runtime installed.

- *HelloWorld.pdb* (optional for deployment)

This is the debug symbols file. You aren't required to deploy this file along with your application, although you should save it in the event that you need to debug the published version of your application.

- *HelloWorld.runtimeconfig.json*

This is the application's runtime configuration file. It identifies the version of .NET that your application was built to run on. You can also add configuration options to it. For more information, see [.NET runtime configuration settings](../runtime-config/index.md#runtimeconfigjson).

::: zone-end

## Run the published app

::: zone pivot="vscode"

1. In **Explorer**, right-click the *publish* folder (<kbd>Ctrl</kbd>-click on macOS), and select **Open in Integrated Terminal**.

:::image type="content" source="media/publishing-with-visual-studio-code/open-in-terminal.png" alt-text="Context menu showing Open in Terminal":::
Expand All @@ -96,12 +184,32 @@ In the following steps, you'll look at the files created by the publish process.

1. Enter a name in response to the prompt, and press <kbd>Enter</kbd> to exit.

::: zone-end

::: zone pivot="codespaces"

1. In **Explorer**, right-click the *artifacts/HelloWorld* folder and select **Open in Integrated Terminal**.

1. Run the app by using the executable. Enter `./HelloWorld` and then press <kbd>Enter</kbd>.

1. Enter a name in response to the prompt, and press <kbd>Enter</kbd> to exit.

::: zone-end

## Additional resources

- [.NET application publishing overview](../deploying/index.md)
- [`dotnet publish`](../tools/dotnet-publish.md)
- [Use the .NET SDK in continuous integration (CI) environments](../../devops/dotnet-cli-and-continuous-integration.md)

::: 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](https://dotnet.microsoft.com/download/dotnet) to download the .NET SDK, you can delete your Codespace. To delete your Codespace, open a browser window and navigate to [your Codespaces](https://github.com/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 published a console app. In the next tutorial, you create a class library.
Expand Down
Loading