diff --git a/docs/core/tutorials/debugging-with-visual-studio-code.md b/docs/core/tutorials/debugging-with-visual-studio-code.md index 5c5d712a4faa7..396b83c1fd04d 100644 --- a/docs/core/tutorials/debugging-with-visual-studio-code.md +++ b/docs/core/tutorials/debugging-with-visual-studio-code.md @@ -1,51 +1,76 @@ --- 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). -## Use Debug build configuration - -*Debug* and *Release* are .NET's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution. +## Set a breakpoint -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. +A *breakpoint* temporarily interrupts the execution of the application before the line with the breakpoint is run. -By default, Visual Studio Code launch settings use the Debug build configuration, so you don't need to change it before debugging. +::: zone pivot="vscode" 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). -## Set a breakpoint - -A *breakpoint* temporarily interrupts the execution of the application before the line with the breakpoint is run. - 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 F9 or choosing **Run** > **Toggle Breakpoint** from the menu while the line of code is selected. Visual Studio Code indicates the line on which the breakpoint is set by displaying a red dot in the left margin. - :::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-set-net6.png" alt-text="Breakpoint set"::: + :::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-set.png" alt-text="Breakpoint set"::: + +::: 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. 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. You can also set a breakpoint are by pressing F9 while the line of code is selected. + + :::image type="content" source="media/debugging-with-visual-studio-code/codespaces-breakpoint-set.png" alt-text="Breakpoint set"::: + +::: zone-end ## Start debugging +*Debug* and *Release* are .NET's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution. + +::: 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. 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"::: + :::image type="content" source="media/debugging-with-visual-studio-code/select-debug-pane.png" alt-text="Open the Debug tab in Visual Studio Code"::: 1. Select **Run and Debug**. If asked, select **C#** and then select **C#: Launch startup project**. Other ways to start the program in debugging mode are by pressing F5 or choosing **Run** > **Start Debugging** from the menu. :::image type="content" source="media/debugging-with-visual-studio-code/start-debugging.png" alt-text="Start debugging"::: -1. If asked to **Select Launch Configuration**, select **C#: HelloWorld HelloWorld**. +1. If asked to **Select Launch Configuration**, select **C#: Debug Active File**. 1. Select the **Debug Console** tab to see the "What is your name?" prompt that the program displays before waiting for a response. @@ -55,7 +80,29 @@ A *breakpoint* temporarily interrupts the execution of the application before th 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/breakpoint-hit-net6.png" alt-text="Breakpoint hit, showing Locals"::: + :::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-hit.png" alt-text="Breakpoint hit, showing Locals"::: + +::: 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 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 Enter. + + 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 @@ -65,9 +112,9 @@ The **Debug Console** window lets you interact with the application you're debug 1. Enter `name = "Gracie"` at the prompt at the bottom of the **Debug Console** window and press Enter. - :::image type="content" source="media/debugging-with-visual-studio-code/change-variable-values-net6.png" alt-text="Change variable values"::: + :::image type="content" source="media/debugging-with-visual-studio-code/change-variable-values.png" alt-text="Change variable values"::: -1. Enter `currentDate = DateTime.Parse("2019-11-16T17:25:00Z").ToUniversalTime()` at the bottom of the **Debug Console** window and press Enter. +1. Enter `currentDate = DateTime.Parse("2026-01-28T20:54:00Z").ToUniversalTime()` at the bottom of the **Debug Console** window and press Enter. The **Variables** window displays the new values of the `name` and `currentDate` variables. @@ -77,7 +124,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 Enter to exit the application and stop debugging. @@ -87,7 +134,7 @@ The program displays the string that the user enters. What happens if the user d 1. Right-click (Ctrl-click on macOS) on the red dot that represents the breakpoint. In the context menu, select **Edit Breakpoint** to open a dialog that lets you enter a conditional expression. - :::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-context-menu-net6.png" alt-text="Breakpoint context menu"::: + :::image type="content" source="media/debugging-with-visual-studio-code/breakpoint-context-menu.png" alt-text="Breakpoint context menu"::: 1. Select `Expression` in the drop-down, enter the following conditional expression, and press Enter. @@ -95,7 +142,7 @@ The program displays the string that the user enters. What happens if the user d String.IsNullOrEmpty(name) ``` - :::image type="content" source="media/debugging-with-visual-studio-code/conditional-expression-net6.png" alt-text="Enter a conditional expression"::: + :::image type="content" source="media/debugging-with-visual-studio-code/conditional-expression.png" alt-text="Enter a conditional expression"::: Each time the breakpoint is hit, the debugger calls the `String.IsNullOrEmpty(name)` method, and it breaks on this line only if the method call returns `true`. @@ -127,7 +174,7 @@ 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 F5 to start debugging. @@ -135,37 +182,35 @@ Visual Studio Code also allows you to step line by line through a program and mo 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 F11. +1. Select **Step Into** from the Debug toolbar or press F11. :::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 F11. - - 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. 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 **Run** > **Step Into** or press F11. +1. Select **Step Into** or press F11. - 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 Enter. The **Debug Console** tab might not display the string you enter while you're entering it, but the method will capture your input. -1. Select **Run** > **Step Into** or press F11. +1. Select **Step Into** or press F11. Visual Studio Code highlights the `currentDate` variable assignment. The **Variables** window shows the value returned by the call to the method. The **Terminal** tab displays the string you entered at the prompt. -1. Select **Run** > **Step Into** or press F11. +1. Select **Step Into** or press F11. The **Variables** window shows the value of the `currentDate` variable after the assignment from the property. -1. Select **Run** > **Step Into** or press F11. +1. Select **Step Into** or press F11. Visual Studio Code calls the method. The console window displays the formatted string. -1. Select **Run** > **Step Out** or press Shift+F11. +1. Select **Step Out** or press Shift+F11. :::image type="content" source="media/debugging-with-visual-studio-code/step-out.png" alt-text="Step-Out button"::: @@ -177,16 +222,41 @@ 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 In this tutorial, you used Visual Studio Code debugging tools. In the next tutorial, you publish a deployable version of the app. diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-context-menu-net6.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-context-menu-net6.png deleted file mode 100644 index 47c21bcae2703..0000000000000 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-context-menu-net6.png and /dev/null differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-context-menu.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-context-menu.png new file mode 100644 index 0000000000000..5fb854b7c144c Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-context-menu.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-hit-net6.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-hit-net6.png deleted file mode 100644 index cb706229ee9e1..0000000000000 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-hit-net6.png and /dev/null differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-hit.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-hit.png new file mode 100644 index 0000000000000..74249ba9c5e54 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-hit.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-set-net6.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-set-net6.png deleted file mode 100644 index 4108b03c8d447..0000000000000 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-set-net6.png and /dev/null differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-set.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-set.png new file mode 100644 index 0000000000000..af38cdbf2041c Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/breakpoint-set.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/change-variable-values-net6.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/change-variable-values-net6.png deleted file mode 100644 index cce287c861176..0000000000000 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/change-variable-values-net6.png and /dev/null differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/change-variable-values.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/change-variable-values.png new file mode 100644 index 0000000000000..52210a3fe29d6 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/change-variable-values.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-breakpoint-hit.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-breakpoint-hit.png new file mode 100644 index 0000000000000..405bcd48258a9 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-breakpoint-hit.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-breakpoint-set.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-breakpoint-set.png new file mode 100644 index 0000000000000..2bd133bf5c058 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-breakpoint-set.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-changed-variable-values.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-changed-variable-values.png new file mode 100644 index 0000000000000..58f44202f6f5a Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-changed-variable-values.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-select-debug-pane.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-select-debug-pane.png new file mode 100644 index 0000000000000..9bb45d077c406 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/codespaces-select-debug-pane.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/conditional-expression-net6.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/conditional-expression-net6.png deleted file mode 100644 index c8bf683804a08..0000000000000 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/conditional-expression-net6.png and /dev/null differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/conditional-expression.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/conditional-expression.png new file mode 100644 index 0000000000000..ac462927bd752 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/conditional-expression.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/continue-debugging.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/continue-debugging.png index ce364de68ea2a..ec9c991207afb 100644 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/continue-debugging.png and b/docs/core/tutorials/media/debugging-with-visual-studio-code/continue-debugging.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-console.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-console.png index 0fbb26f2f429e..db0437a631bb5 100644 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-console.png and b/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-console.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-pane-net6.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-pane-net6.png deleted file mode 100644 index 081bb61db19f0..0000000000000 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-pane-net6.png and /dev/null differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-pane.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-pane.png new file mode 100644 index 0000000000000..77bddf0641c01 Binary files /dev/null and b/docs/core/tutorials/media/debugging-with-visual-studio-code/select-debug-pane.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/start-debugging.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/start-debugging.png index e7227949661b3..2ddea0e6fd5ec 100644 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/start-debugging.png and b/docs/core/tutorials/media/debugging-with-visual-studio-code/start-debugging.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/step-into.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/step-into.png index e2686ea83f1af..fc9731bc53c86 100644 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/step-into.png and b/docs/core/tutorials/media/debugging-with-visual-studio-code/step-into.png differ diff --git a/docs/core/tutorials/media/debugging-with-visual-studio-code/step-out.png b/docs/core/tutorials/media/debugging-with-visual-studio-code/step-out.png index a55550da55dde..f143d2c6c2407 100644 Binary files a/docs/core/tutorials/media/debugging-with-visual-studio-code/step-out.png and b/docs/core/tutorials/media/debugging-with-visual-studio-code/step-out.png differ diff --git a/docs/core/tutorials/media/publishing-with-visual-studio-code/codespaces-published-files-output.png b/docs/core/tutorials/media/publishing-with-visual-studio-code/codespaces-published-files-output.png new file mode 100644 index 0000000000000..4bc254d6bdab4 Binary files /dev/null and b/docs/core/tutorials/media/publishing-with-visual-studio-code/codespaces-published-files-output.png differ diff --git a/docs/core/tutorials/media/publishing-with-visual-studio-code/published-files-output-net8.png b/docs/core/tutorials/media/publishing-with-visual-studio-code/published-files-output-net8.png deleted file mode 100644 index e5804c63d1e08..0000000000000 Binary files a/docs/core/tutorials/media/publishing-with-visual-studio-code/published-files-output-net8.png and /dev/null differ diff --git a/docs/core/tutorials/media/publishing-with-visual-studio-code/published-files-output.png b/docs/core/tutorials/media/publishing-with-visual-studio-code/published-files-output.png new file mode 100644 index 0000000000000..ccdbde7193f52 Binary files /dev/null and b/docs/core/tutorials/media/publishing-with-visual-studio-code/published-files-output.png differ diff --git a/docs/core/tutorials/media/with-visual-studio-code/codespaces-create-new-file.png b/docs/core/tutorials/media/with-visual-studio-code/codespaces-create-new-file.png new file mode 100644 index 0000000000000..638a47be64430 Binary files /dev/null and b/docs/core/tutorials/media/with-visual-studio-code/codespaces-create-new-file.png differ diff --git a/docs/core/tutorials/media/with-visual-studio-code/create-codespace-on-main.png b/docs/core/tutorials/media/with-visual-studio-code/create-codespace-on-main.png new file mode 100644 index 0000000000000..e67036208c345 Binary files /dev/null and b/docs/core/tutorials/media/with-visual-studio-code/create-codespace-on-main.png differ diff --git a/docs/core/tutorials/media/with-visual-studio-code/create-dotnet-project.png b/docs/core/tutorials/media/with-visual-studio-code/create-dotnet-project.png new file mode 100644 index 0000000000000..336ccf1902aec Binary files /dev/null and b/docs/core/tutorials/media/with-visual-studio-code/create-dotnet-project.png differ diff --git a/docs/core/tutorials/media/with-visual-studio-code/run-program-class.png b/docs/core/tutorials/media/with-visual-studio-code/run-program-class.png new file mode 100644 index 0000000000000..047f1a0e55d4b Binary files /dev/null and b/docs/core/tutorials/media/with-visual-studio-code/run-program-class.png differ diff --git a/docs/core/tutorials/publishing-with-visual-studio-code.md b/docs/core/tutorials/publishing-with-visual-studio-code.md index 1763c8e82c5a6..884e954b9cd75 100644 --- a/docs/core/tutorials/publishing-with-visual-studio-code.md +++ b/docs/core/tutorials/publishing-with-visual-studio-code.md @@ -1,13 +1,14 @@ --- 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 @@ -15,6 +16,8 @@ The .NET CLI is used to publish the app, so you can follow this tutorial with a ## 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). @@ -34,25 +37,60 @@ The .NET CLI is used to publish the app, so you can follow this tutorial with a The command output is similar to the following example: ```output - Microsoft (R) Build Engine version 17.8.0+b89cb5fde for .NET - Copyright (C) Microsoft Corporation. All rights reserved. - Determining projects to restore... - All projects are up-to-date for restore. - HelloWorld -> C:\Projects\HelloWorld\bin\Release\net8.0\HelloWorld.dll - HelloWorld -> C:\Projects\HelloWorld\bin\Release\net8.0\publish\ + Restore complete (1.1s) + HelloWorld net10.0 succeeded (7.8s) → bin\Release\net10.0\publish\ + + Build succeeded in 10.3s ``` +::: 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. + +1. In the terminal, make sure you're in the *tutorials* folder. + +1. Run the following command: + + ```dotnetcli + dotnet publish HelloWorld.cs + ``` + + 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. 1. Select the **Explorer** in the left navigation bar. -1. Expand *bin/Release/net8.0/publish*. +1. Expand *bin/Release/net10.0/publish*. - :::image type="content" source="media/publishing-with-visual-studio-code/published-files-output-net8.png" alt-text="Explorer showing published files"::: + :::image type="content" source="media/publishing-with-visual-studio-code/published-files-output.png" alt-text="Explorer showing published files"::: As the image shows, the published output includes the following files: @@ -66,7 +104,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) @@ -76,8 +114,50 @@ 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. Codespaces runs on Linux, so this a Linux executable. + + - *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). + + Right-click and select **Download...** to download files from Codespaces to your local computer. + +::: zone-end + ## Run the published app +::: zone pivot="vscode" + 1. In **Explorer**, right-click the *publish* folder (Ctrl-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"::: @@ -96,12 +176,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 Enter 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 Enter. + +1. Enter a name in response to the prompt, and press Enter 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. diff --git a/docs/core/tutorials/snippets/with-visual-studio-code/csharp/HelloWorld.cs b/docs/core/tutorials/snippets/with-visual-studio-code/csharp/HelloWorld.cs new file mode 100644 index 0000000000000..5255638abec71 --- /dev/null +++ b/docs/core/tutorials/snippets/with-visual-studio-code/csharp/HelloWorld.cs @@ -0,0 +1,12 @@ +// +Console.WriteLine("Hello, World!"); +// + +// +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 Enter to exit..."); +Console.Read(); +// \ No newline at end of file diff --git a/docs/core/tutorials/with-visual-studio-code.md b/docs/core/tutorials/with-visual-studio-code.md index 1aaf7618017d7..e4e728a63a88e 100644 --- a/docs/core/tutorials/with-visual-studio-code.md +++ b/docs/core/tutorials/with-visual-studio-code.md @@ -1,71 +1,152 @@ --- title: Create a .NET console application using Visual Studio Code description: Learn how to create a .NET console application using Visual Studio Code. -ms.date: 11/22/2024 +ms.date: 01/26/2026 +zone_pivot_groups: code-editor-set-one --- # Tutorial: Create a .NET console application using Visual Studio Code +::: 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 single-file app. +> * Enhance the app to prompt the user for their name and display it in the console window. + +::: zone-end + ## Prerequisites +::: zone pivot="vscode" + [!INCLUDE [Prerequisites](../../../includes/prerequisites-basic-winget.md)] +::: zone-end + +::: zone pivot="codespaces" + +- A GitHub account to use [GitHub Codespaces](https://github.com/codespaces). If you don't already have one, you can create a free account at [GitHub.com](https://github.com). + +::: zone-end + ## Create the app +::: zone pivot="vscode" + Create a .NET console app project named "HelloWorld". 1. Start Visual Studio Code. 1. 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/with-visual-studio-code/create-dotnet-project.png" alt-text="The .NET: New Project command in the Command Palette"::: + 1. After selecting the command, you need to choose the project template. Choose **Console App**. 1. Select the location where you would like the new project to be created. 1. Give your new project a name, "HelloWorld". -1. Select to **Show all template options**. Set **Do not use top-level statements** to **true**. And finally, select **Create Project**. +1. Select **.sln** for the solution file format. -1. In the **Do you trust the authors of the files in this folder?** dialog, select **Yes, I trust the authors**. You can trust the authors because this folder only has files generated by .NET and added or modified by you. +1. Select **Create Project**. -1. Open the *Program.cs* file to see the simple application created by the template: +1. The project is created and the *Program.cs* file opens. You see the simple application created by the template: ```csharp - namespace HelloWorld; - - class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } - } + // See https://aka.ms/new-console-template for more information + Console.WriteLine("Hello, World!"); ``` - The code defines a class, `Program`, with a single method, `Main`, that takes a 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 the *args* array. The code in `Main` calls the method to display a message in the console window. + The code defines a class, `Program`, that calls the method to display a message in the console window. + +::: zone-end - C# has a feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) that lets you omit the `Program` class and the `Main` method. This tutorial doesn't use this feature. Whether you use it in your programs is a matter of style preference. By setting **Do not use top-level statements to true** when you created the project, you prevented top-level statements from being used. +::: zone pivot="codespaces" + +### Open Codespaces + +Start a GitHub Codespace with the tutorial environment. + +1. Open a browser window and navigate to the [tutorial codespace](https://github.com/dotnet/tutorial-codespace) repository. + +1. Select the green **Code** button, and then the **Codespaces** tab. + +1. Select the `+` sign or the green **Create codespace on main** button to create a new Codespace using this environment. + + :::image type="content" source="media/with-visual-studio-code/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](../sdk/file-based-apps.md). 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/with-visual-studio-code/codespaces-create-new-file.png" alt-text="Create a new file named HelloWorld.cs in the tutorials folder"::: + +1. *HelloWorld.cs* opens in the editor. Type or copy the following code into the file: + + :::code language="csharp" source="./snippets/with-visual-studio-code/csharp/HelloWorld.cs" id="HelloWorld"::: + +::: zone-end ## Run the app +::: 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#**, then select **C#: HelloWorld** +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: + +```dotnetcli +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="vscode" + 1. Open *Program.cs*. -1. Replace the contents of the `Main` method in *Program.cs*, which is the line that calls `Console.WriteLine`, with the following code: +1. Replace the contents of the class with the following code: :::code language="csharp" source="./snippets/with-visual-studio/csharp/Program-Read.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 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 method to wait for user input. + 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 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 method to wait for user input. is a platform-independent and language-independent way to represent a line break. It's the same as `\n` in C#. @@ -80,14 +161,63 @@ Enhance the application to prompt the user for their name and display it along w 1. Respond to the prompt by entering a name and pressing the Enter key. - :::image type="content" source="media/debugging-with-visual-studio-code/run-modified-program.png" alt-text="Terminal window with modified program output"::: + :::image type="content" source="media/with-visual-studio-code/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/with-visual-studio-code/csharp/HelloWorld.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 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 method to wait for user input. + + is a platform-independent and language-independent way to represent a line break. It's the same as `\n` in C#. + + 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](../../csharp/language-reference/tokens/interpolated.md). + +1. Run the updated app using the following command: + + ```dotnetcli + dotnet HelloWorld.cs + ``` + +1. Respond to the prompt by entering a name and pressing the Enter key. + + You'll see output similar to the following: + + ```output + 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. -1. Press Enter to exit the program. +::: zone-end ## Additional resources +::: zone pivot="vscode" + * [Setting up Visual Studio Code](https://code.visualstudio.com/docs/setup/setup-overview) +::: zone-end + +::: zone pivot="codespaces" + +* [GitHub Codespaces documentation](https://docs.github.com/codespaces) +* [Getting started with GitHub Codespaces](https://docs.github.com/codespaces/getting-started) + +## 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 created a .NET console application. In the next tutorial, you debug the app. diff --git a/docs/zone-pivot-groups.yml b/docs/zone-pivot-groups.yml index 4cb474bfa2ac2..41cf8432c85d1 100644 --- a/docs/zone-pivot-groups.yml +++ b/docs/zone-pivot-groups.yml @@ -160,3 +160,11 @@ groups: title: OpenAI - id: ollama title: Ollama +- id: code-editor-set-one + title: Code editor + prompt: Choose a code editor + pivots: + - id: vscode + title: Visual Studio Code + - id: codespaces + title: GitHub Codespaces