From b0d87780b06ca4bfc5f957a7f8e24489962c3eab Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:32:24 -0800 Subject: [PATCH 1/4] remove .net 6/7 zone pivots --- .../cli-templates-create-template-package.md | 91 +--- .../debugging-with-visual-studio-code.md | 391 ----------------- .../tutorials/debugging-with-visual-studio.md | 366 ---------------- .../library-with-visual-studio-code.md | 271 ------------ .../tutorials/library-with-visual-studio.md | 273 ------------ .../publishing-with-visual-studio-code.md | 223 ---------- .../publishing-with-visual-studio.md | 229 ---------- ...testing-library-with-visual-studio-code.md | 311 -------------- .../testing-library-with-visual-studio.md | 403 ------------------ .../core/tutorials/with-visual-studio-code.md | 242 ----------- docs/core/tutorials/with-visual-studio.md | 237 ---------- .../migrate-from-newtonsoft.md | 2 +- docs/zone-pivot-groups.yml | 4 - 13 files changed, 2 insertions(+), 3041 deletions(-) diff --git a/docs/core/tutorials/cli-templates-create-template-package.md b/docs/core/tutorials/cli-templates-create-template-package.md index bffcfda3155be..9b0fe9fbdc373 100644 --- a/docs/core/tutorials/cli-templates-create-template-package.md +++ b/docs/core/tutorials/cli-templates-create-template-package.md @@ -3,7 +3,6 @@ title: Create a template package for dotnet new description: Learn how to create a csproj file that builds a template package for the dotnet new command. author: adegeo ms.date: 09/11/2023 -zone_pivot_groups: dotnet-version ms.topic: tutorial ms.author: adegeo --- @@ -16,27 +15,12 @@ You can view the completed template in the [.NET Samples GitHub repository](http In this part of the series you'll learn how to: -::: zone pivot="dotnet-9-0,dotnet-8-0" - > [!div class="checklist"] > > * Create a template package by using the [Microsoft.TemplateEngine.Authoring.Templates](https://www.nuget.org/packages/Microsoft.TemplateEngine.Authoring.Templates) NuGet package. > * Install a template package from a NuGet package file. > * Uninstall a template package by package ID. -::: zone-end - -::: zone pivot="dotnet-7-0,dotnet-6-0" - -> [!div class="checklist"] -> -> * Create a \*.csproj project to build a template package. -> * Configure the project file for packing. -> * Install a template package from a NuGet package file. -> * Uninstall a template package by package ID. - -::: zone-end - ## Prerequisites * Complete [part 1](cli-templates-create-item-template.md) and [part 2](cli-templates-create-project-template.md) of this tutorial series. @@ -45,21 +29,11 @@ In this part of the series you'll learn how to: * Open a terminal and navigate to the _working_ folder. -::: zone pivot="dotnet-8-0" - -* Install .NET 8. +* Install .NET 8 or .NET 9. * Install the `Microsoft.TemplateEngine.Authoring.Templates` template from the NuGet package feed. * Run the `dotnet new install Microsoft.TemplateEngine.Authoring.Templates` command from your terminal. -::: zone-end - -::: zone pivot="dotnet-7-0,dotnet-6-0" - -[!INCLUDE [dotnet6-syntax-note](includes/dotnet6-syntax-note.md)] - -::: zone-end - ## Create a template package project A template package is one or more templates packed into a NuGet package. When you install or uninstall a template package, all templates contained in the package are added or removed, respectively. @@ -70,8 +44,6 @@ Normally you use a C# project file to compile code and produce a binary. However The package you're going to generate will include the [item](cli-templates-create-item-template.md) and [project](cli-templates-create-project-template.md) templates previously created. -::: zone pivot="dotnet-8-0" - The [Microsoft.TemplateEngine.Authoring.Templates](https://www.nuget.org/packages/Microsoft.TemplateEngine.Authoring.Templates) package contains templates useful for template authoring. To install this package, nuget.org should be available as NuGet feed in the working directory. 01. In the _working_ folder, run the following command to create the template package: @@ -116,63 +88,6 @@ The [Microsoft.TemplateEngine.Authoring.Templates](https://www.nuget.org/package ... cut for brevity ... ``` -::: zone-end - -::: zone pivot="dotnet-7-0,dotnet-6-0" - -01. In the _working_ folder, run the following command to create the template package: - - ```dotnetcli - dotnet new console -n AdatumCorporation.Utility.Templates - ``` - - The `-n` parameter sets the project file name to _AdatumCorporation.Utility.Templates.csproj_. You should see a result similar to the following output. - - ```output - The template "Console Application" was created successfully. - - Processing post-creation actions... - Running 'dotnet restore' on .\AdatumCorporation.Utility.Templates.csproj... - Restore completed in 52.38 ms for C:\code\working\AdatumCorporation.Utility.Templates.csproj. - - Restore succeeded. - ``` - -01. Delete the _Program.cs_ file. The new project template generates this file but it's not used by the templates engine. - -01. Next, open the _AdatumCorporation.Utility.Templates.csproj_ file in your favorite editor and replace the content with the following XML: - - ```xml - - - - AdatumCorporation.Utility.Templates - 1.0 - AdatumCorporation Templates - Me - Templates to use when creating an application for Adatum Corporation. - dotnet-new;templates;adatum - https://your-url - - Template - netstandard2.0 - true - false - content - $(NoWarn);NU5128 - true - - - - - - - - - ``` - -::: zone-end - ### Description of the project XML The settings under `` in the XML snippet are broken into two groups. @@ -193,8 +108,6 @@ In the second group, the `` setting ensures that MSBuild runs p > [!TIP] > For more information about NuGet metadata settings, see [Pack a template into a NuGet package (nupkg file)](../tools/custom-templates.md#pack-a-template-into-a-nuget-package-nupkg-file). -::: zone pivot="dotnet-8-0" - The created project file includes [template authoring MSBuild tasks](https://aka.ms/templating-authoring-tools) and localization settings. ```xml @@ -212,8 +125,6 @@ The created project file includes [template authoring MSBuild tasks](https://aka These MSBuild tasks provide template validation and [localization of the templates](https://aka.ms/templating-localization) capabilities. Localization is disabled by default. To enable creation of localization files, set `LocalizeTemplates` to `true`. -::: zone-end - ## Pack and install Save the project file. Before building the template package, verify that your folder structure is correct. Any template you want to pack should be placed in the _templates_ folder, in its own folder. The folder structure should look similar to the following hierarchy: diff --git a/docs/core/tutorials/debugging-with-visual-studio-code.md b/docs/core/tutorials/debugging-with-visual-studio-code.md index 48bf5bc95ab78..46f1273a979e9 100644 --- a/docs/core/tutorials/debugging-with-visual-studio-code.md +++ b/docs/core/tutorials/debugging-with-visual-studio-code.md @@ -2,12 +2,9 @@ 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: 09/12/2024 -zone_pivot_groups: dotnet-version --- # Tutorial: Debug a .NET console application using Visual Studio Code -::: zone pivot="dotnet-9-0,dotnet-8-0" - This tutorial introduces the debugging tools available in Visual Studio Code for working with .NET apps. ## Prerequisites @@ -196,391 +193,3 @@ In this tutorial, you used Visual Studio Code debugging tools. In the next tutor > [!div class="nextstepaction"] > [Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -This tutorial introduces the debugging tools available in Visual Studio Code for working with .NET apps. - -## 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. - -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. - -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). - -## 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"::: - -## Start 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"::: - -1. Select the green arrow at the top of the pane, next to **.NET Core Launch (console)**. 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. Select the **Debug Console** tab to see the "What is your name?" prompt that the program displays before waiting for a response. - - :::image type="content" source="media/debugging-with-visual-studio-code/select-terminal-net6.png" alt-text="Select the Terminal tab"::: - -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/breakpoint-hit-net6.png" alt-text="Breakpoint hit, showing Locals"::: - -## 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. - -1. Select the **Debug Console** tab. - -1. Enter `name = "Gracie"` at the prompt at the bottom of the **Debug Console** window and press the Enter key. - - :::image type="content" source="media/debugging-with-visual-studio-code/change-variable-values-net6.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 the Enter key. - - The **Variables** window displays the new values of the `name` and `currentDate` variables. - -1. Continue program execution by selecting the **Continue** button in the toolbar. Another way to continue is by pressing F5. - - :::image type="content" source="media/debugging-with-visual-studio-code/continue-debugging.png" alt-text="Continue debugging"::: - - 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"::: - -1. Press any key to exit the application and stop debugging. - -## Set a conditional breakpoint - -The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a *conditional breakpoint*. - -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"::: - -1. Select `Expression` in the drop-down, enter the following conditional expression, and press Enter. - - ```csharp - String.IsNullOrEmpty(name) - ``` - - :::image type="content" source="media/debugging-with-visual-studio-code/conditional-expression-net6.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`. - - Instead of a conditional expression, you can specify a *hit count*, which interrupts program execution before a statement is run a specified number of times. Another option is to specify a *filter condition*, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name. - -1. Start the program with debugging by pressing F5. - -1. In the **Debug Console** tab, press the Enter key when prompted to enter your name. - - Because the condition you specified (`name` is either `null` or ) has been satisfied, program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method runs. - - The **Variables** window shows that the value of the `name` variable is `""`, or . - -1. Confirm the value is an empty string by entering the following statement at the **Debug Console** prompt and pressing Enter. The result is `true`. - - ```csharp - name == String.Empty - ``` - -1. Select the **Continue** button on the toolbar to continue program execution. - -1. Press any key to exit the program and stop debugging. - -1. Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing **Run > Toggle Breakpoint** from the menu while the line of code is selected. - -1. If you get a warning that the breakpoint condition will be lost, select **Remove Breakpoint**. - -## Step through a program - -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. Press F5 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 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. Select **Run** > **Step Into** or press F11. - - Visual Studio 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. - - 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. - - The **Variables** window shows the value of the `currentDate` variable after the assignment from the property. - -1. Select **Run** > **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. - - :::image type="content" source="media/debugging-with-visual-studio-code/step-out.png" alt-text="Step-Out button"::: - -1. Select the **Terminal** tab. - - The terminal displays "Press any key to exit..." - -1. Press any key to exit the program. - -## Use Release build configuration - -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. - -To build and test the Release version of your console application, open the **Terminal** and run the following command: - -```dotnetcli -dotnet run --configuration Release -``` - -## Additional resources - -- [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging) - -## Next steps - -In this tutorial, you used Visual Studio Code debugging tools. In the next tutorial, you publish a deployable version of the app. - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -This tutorial introduces the debugging tools available in Visual Studio Code for working with .NET apps. - -## 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. - -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. - -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). - -## 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"::: - -## Start 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"::: - -1. Select the green arrow at the top of the pane, next to **.NET Core Launch (console)**. 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. Select the **Debug Console** tab to see the "What is your name?" prompt that the program displays before waiting for a response. - - :::image type="content" source="media/debugging-with-visual-studio-code/select-terminal-net6.png" alt-text="Select the Terminal tab"::: - -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/breakpoint-hit-net6.png" alt-text="Breakpoint hit, showing Locals"::: - -## 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. - -1. Select the **Debug Console** tab. - -1. Enter `name = "Gracie"` at the prompt at the bottom of the **Debug Console** window and press the Enter key. - - :::image type="content" source="media/debugging-with-visual-studio-code/change-variable-values-net6.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 the Enter key. - - The **Variables** window displays the new values of the `name` and `currentDate` variables. - -1. Continue program execution by selecting the **Continue** button in the toolbar. Another way to continue is by pressing F5. - - :::image type="content" source="media/debugging-with-visual-studio-code/continue-debugging.png" alt-text="Continue debugging"::: - - 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"::: - -1. Press any key to exit the application and stop debugging. - -## Set a conditional breakpoint - -The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a *conditional breakpoint*. - -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"::: - -1. Select `Expression` in the drop-down, enter the following conditional expression, and press Enter. - - ```csharp - String.IsNullOrEmpty(name) - ``` - - :::image type="content" source="media/debugging-with-visual-studio-code/conditional-expression-net6.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`. - - Instead of a conditional expression, you can specify a *hit count*, which interrupts program execution before a statement is run a specified number of times. Another option is to specify a *filter condition*, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name. - -1. Start the program with debugging by pressing F5. - -1. In the **Debug Console** tab, press the Enter key when prompted to enter your name. - - Because the condition you specified (`name` is either `null` or ) has been satisfied, program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method runs. - - The **Variables** window shows that the value of the `name` variable is `""`, or . - -1. Confirm the value is an empty string by entering the following statement at the **Debug Console** prompt and pressing Enter. The result is `true`. - - ```csharp - name == String.Empty - ``` - -1. Select the **Continue** button on the toolbar to continue program execution. - -1. Press any key to exit the program and stop debugging. - -1. Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing **Run > Toggle Breakpoint** from the menu while the line of code is selected. - -1. If you get a warning that the breakpoint condition will be lost, select **Remove Breakpoint**. - -## Step through a program - -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. Press F5 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 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. Select **Run** > **Step Into** or press F11. - - Visual Studio highlights the `name` variable assignment. The **Variables** window is cleared. - -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. - - The **Variables** window shows the value returned by the call to the method. - -1. Select **Run** > **Step Into** or press F11. - - Visual Studio Code highlights the `currentDate` variable assignment. The **Terminal** tab displays the string you entered at the prompt. - -1. Select **Run** > **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. - - Visual Studio Code calls the method. The console window displays the formatted string. - -1. Select **Run** > **Step Out** or press Shift+F11. - - :::image type="content" source="media/debugging-with-visual-studio-code/step-out.png" alt-text="Step-Out button"::: - - The terminal displays "Press any key to exit..." - -1. Press any key to exit the program. - -## Use Release build configuration - -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. - -To build and test the Release version of your console application, open the **Terminal** and run the following command: - -```dotnetcli -dotnet run --configuration Release -``` - -## Additional resources - -- [Debugging in Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging) - -## Next steps - -In this tutorial, you used Visual Studio Code debugging tools. In the next tutorial, you publish a deployable version of the app. - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) - -::: zone-end diff --git a/docs/core/tutorials/debugging-with-visual-studio.md b/docs/core/tutorials/debugging-with-visual-studio.md index d3976eab8e422..3286db99dbb77 100644 --- a/docs/core/tutorials/debugging-with-visual-studio.md +++ b/docs/core/tutorials/debugging-with-visual-studio.md @@ -2,7 +2,6 @@ title: Debug a .NET console application using Visual Studio description: Learn how to debug a .NET console app using Visual Studio. ms.date: 08/21/2023 -zone_pivot_groups: dotnet-version dev_langs: - "csharp" - "vb" @@ -10,190 +9,6 @@ ms.custom: "vs-dotnet" --- # Tutorial: Debug a .NET console application using Visual Studio -::: zone pivot="dotnet-9-0,dotnet-8-0" - -This tutorial introduces the debugging tools available in Visual Studio. - -> [!IMPORTANT] -> All of the keyboard shortcuts are based on the defaults from Visual Studio. Your keyboard shortcuts may vary, for more information see [Keyboard shortcuts in Visual Studio](/visualstudio/ide/default-keyboard-shortcuts-in-visual-studio). - -## Prerequisites - -- This tutorial works with the console app that you create in [Create a .NET console application using Visual Studio](with-visual-studio.md). - -## Use Debug build configuration - -*Debug* and *Release* are Visual Studio's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution. - -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. - - By default, Visual Studio uses the Debug build configuration, so you don't need to change it before debugging. - -1. Start Visual Studio. - -1. Open the project that you created in [Create a .NET console application using Visual Studio](with-visual-studio.md). - - The current build configuration is shown on the toolbar. The following toolbar image shows that Visual Studio is configured to compile the Debug version of the app: - - :::image type="content" source="./media/debugging-with-visual-studio/visual-studio-toolbar-debug.png" alt-text="Visual Studio toolbar with debug highlighted"::: - -## Set a breakpoint - -A *breakpoint* temporarily interrupts the execution of the application before the line with the breakpoint is executed. - -1. Set a *breakpoint* on the line that displays the name, date, and time, by clicking in the left margin of the code window on that line. The left margin is to the left of the line numbers. Other ways to set a breakpoint are by placing the cursor in the line of code and then pressing F9 or choosing **Debug** > **Toggle Breakpoint** from the menu bar. - - As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin. - - :::image type="content" source="./media/debugging-with-visual-studio/set-breakpoint-in-editor-net6.png" alt-text="Visual Studio Program window with breakpoint set"::: - -1. Press F5 to run the program in Debug mode. Another way to start debugging is by choosing **Debug** > **Start Debugging** from the menu. - -1. Enter a string in the console window when the program prompts for a name, and then press Enter. - -1. Program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method executes. The **Locals** window displays the values of variables that are defined in the currently executing method. - - :::image type="content" source="./media/debugging-with-visual-studio/breakpoint-hit-net6.png" alt-text="Screenshot of a breakpoint in Visual Studio"::: - -## Use the Immediate window - -The **Immediate** window lets you interact with the application you're debugging. You can interactively change the value of variables to see how it affects your program. - -1. If the **Immediate** window is not visible, display it by choosing **Debug** > **Windows** > **Immediate**. - -1. Enter `name = "Gracie"` in the **Immediate** window and press the Enter key. - -1. Enter `currentDate = DateTime.Parse("2019-11-16T17:25:00Z").ToUniversalTime()` in the **Immediate** window and press the Enter key. - - The **Immediate** window displays the value of the string variable and the properties of the value. In addition, the values of the variables are updated in the **Locals** window. - - :::image type="content" source="./media/debugging-with-visual-studio/locals-immediate-window.png" alt-text="Locals and Immediate Windows in Visual Studio 2019"::: - -1. Press F5 to continue program execution. Another way to continue is by choosing **Debug** > **Continue** from the menu. - - The values displayed in the console window correspond to the changes you made in the **Immediate** window. - - :::image type="content" source="./media/debugging-with-visual-studio/console-window.png" alt-text="Console window showing the entered values"::: - -1. Press any key to exit the application and stop debugging. - -## Set a conditional breakpoint - -The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a *conditional breakpoint*. - -1. Right-click on the red dot that represents the breakpoint. In the context menu, select **Conditions** to open the **Breakpoint Settings** dialog. Select the box for **Conditions** if it's not already selected. - - :::image type="content" source="./media/debugging-with-visual-studio/breakpoint-settings-net6.png" alt-text="Editor showing breakpoint settings panel - C#"::: - -1. For the **Conditional Expression**, enter the following code in the field that shows example code that tests if `x` is 5. - - ```csharp - string.IsNullOrEmpty(name) - ``` - - ```vb - String.IsNullOrEmpty(name) - ``` - - 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`. - - Instead of a conditional expression, you can specify a *hit count*, which interrupts program execution before a statement is executed a specified number of times. Another option is to specify a *filter condition*, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name. - -1. Select **Close** to close the dialog. - -1. Start the program with debugging by pressing F5. - -1. In the console window, press the Enter key when prompted to enter your name. - -1. Because the condition you specified (`name` is either `null` or ) has been satisfied, program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method executes. - -1. Select the **Locals** window, which shows the values of variables that are local to the currently executing method. In this case, `Main` is the currently executing method. Observe that the value of the `name` variable is `""`, or . - -1. Confirm the value is an empty string by entering the following statement in the **Immediate** window and pressing Enter. The result is `true`. - - ```csharp - ? name == String.Empty - ``` - - ```vb - ? String.IsNullOrEmpty(name) - ``` - - The question mark directs the immediate window to [evaluate an expression](/visualstudio/ide/reference/immediate-window#enter-commands). - - :::image type="content" source="./media/debugging-with-visual-studio/immediate-window-output.png" alt-text="Immediate Window returning a value of true after the statement is executed - C#"::: - -1. Press F5 to continue program execution. - -1. Press any key to close the console window and stop debugging. - -1. Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing **Debug > Toggle Breakpoint** while the line of code is selected. - -## Step through a program - -Visual Studio 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. Choose **Debug** > **Step Into**. Another way to debug one statement at a time is by pressing F11. - - Visual Studio highlights and displays an arrow beside the next line of execution. - - C# - - :::image type="content" source="./media/debugging-with-visual-studio/step-into-method-net6.png" alt-text="Visual Studio step into method - C#"::: - - Visual Basic - - :::image type="content" source="./media/debugging-with-visual-studio/vb-step-into-method.png" alt-text="Visual Studio step into method - Visual Basic"::: - - At this point, the **Locals** window shows that the `args` array is empty, and `name` and `currentDate` have default values. In addition, Visual Studio has opened a blank console window. - -1. Press F11. Visual Studio now highlights the next line of execution. The **Locals** window is unchanged, and the console window remains blank. - - C# - - :::image type="content" source="./media/debugging-with-visual-studio/step-into-source-method-net6.png" alt-text="Visual Studio step in method source - C#"::: - - Visual Basic - - :::image type="content" source="./media/debugging-with-visual-studio/vb-step-into-source-method.png" alt-text="Visual Studio step into method source - Visual Basic"::: - -1. Press F11. Visual Studio highlights the statement that includes the `name` variable assignment. The **Locals** window shows that `name` is `null`, and the console window displays the string "What is your name?". - -1. Respond to the prompt by entering a string in the console window and pressing Enter. The console is unresponsive, and the string you entered isn't displayed in the console window, but the method will nevertheless capture your input. - -1. Press F11. Visual Studio highlights the statement that includes the `currentDate` variable assignment. The **Locals** window shows the value returned by the call to the method. The console window also displays the string you entered at the prompt. - -1. Press F11. The **Locals** window shows the value of the `currentDate` variable after the assignment from the property. The console window is unchanged. - -1. Press F11. Visual Studio calls the method. The console window displays the formatted string. - -1. Choose **Debug** > **Step Out**. Another way to stop step-by-step execution is by pressing Shift+F11. - - The console window displays a message and waits for you to press a key. - -1. Press any key to close the console window and stop debugging. - -## Use Release build configuration - -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 sometimes negatively affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications. - -To build and test the Release version of your console application, change the build configuration on the toolbar from **Debug** to **Release**. - -:::image type="content" source="./media/debugging-with-visual-studio/visual-studio-toolbar-release.png" alt-text="default Visual Studio toolbar with release highlighted"::: - -When you press F5 or choose **Build Solution** from the **Build** menu, Visual Studio compiles the Release version of the application. You can test it as you did the Debug version. - -## Next steps - -In this tutorial, you used Visual Studio debugging tools. In the next tutorial, you publish a deployable version of the app. - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - This tutorial introduces the debugging tools available in Visual Studio. > [!IMPORTANT] @@ -371,184 +186,3 @@ In this tutorial, you used Visual Studio debugging tools. In the next tutorial, > [!div class="nextstepaction"] > [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -This tutorial introduces the debugging tools available in Visual Studio. - -## Prerequisites - -- This tutorial works with the console app that you create in [Create a .NET console application using Visual Studio](with-visual-studio.md). - -## Use Debug build configuration - -*Debug* and *Release* are Visual Studio's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution. - -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. - - By default, Visual Studio uses the Debug build configuration, so you don't need to change it before debugging. - -1. Start Visual Studio. - -1. Open the project that you created in [Create a .NET console application using Visual Studio](with-visual-studio.md). - - The current build configuration is shown on the toolbar. The following toolbar image shows that Visual Studio is configured to compile the Debug version of the app: - - :::image type="content" source="./media/debugging-with-visual-studio/visual-studio-toolbar-debug.png" alt-text="Visual Studio toolbar with debug highlighted"::: - -## Set a breakpoint - -A *breakpoint* temporarily interrupts the execution of the application before the line with the breakpoint is executed. - -1. Set a *breakpoint* on the line that displays the name, date, and time, by clicking in the left margin of the code window on that line. The left margin is to the left of the line numbers. Other ways to set a breakpoint are by placing the cursor in the line of code and then pressing F9 or choosing **Debug** > **Toggle Breakpoint** from the menu bar. - - As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin. - - :::image type="content" source="./media/debugging-with-visual-studio/set-breakpoint-in-editor-net6.png" alt-text="Visual Studio Program window with breakpoint set"::: - -1. Press F5 to run the program in Debug mode. Another way to start debugging is by choosing **Debug** > **Start Debugging** from the menu. - -1. Enter a string in the console window when the program prompts for a name, and then press Enter. - -1. Program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method executes. The **Locals** window displays the values of variables that are defined in the currently executing method. - - :::image type="content" source="./media/debugging-with-visual-studio/breakpoint-hit-net6.png" alt-text="Screenshot of a breakpoint in Visual Studio"::: - -## Use the Immediate window - -The **Immediate** window lets you interact with the application you're debugging. You can interactively change the value of variables to see how it affects your program. - -1. If the **Immediate** window is not visible, display it by choosing **Debug** > **Windows** > **Immediate**. - -1. Enter `name = "Gracie"` in the **Immediate** window and press the Enter key. - -1. Enter `currentDate = DateTime.Parse("2019-11-16T17:25:00Z").ToUniversalTime()` in the **Immediate** window and press the Enter key. - - The **Immediate** window displays the value of the string variable and the properties of the value. In addition, the values of the variables are updated in the **Locals** window. - - :::image type="content" source="./media/debugging-with-visual-studio/locals-immediate-window.png" alt-text="Locals and Immediate Windows in Visual Studio 2019"::: - -1. Press F5 to continue program execution. Another way to continue is by choosing **Debug** > **Continue** from the menu. - - The values displayed in the console window correspond to the changes you made in the **Immediate** window. - - :::image type="content" source="./media/debugging-with-visual-studio/console-window.png" alt-text="Console window showing the entered values"::: - -1. Press any key to exit the application and stop debugging. - -## Set a conditional breakpoint - -The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a *conditional breakpoint*. - -1. Right-click on the red dot that represents the breakpoint. In the context menu, select **Conditions** to open the **Breakpoint Settings** dialog. Select the box for **Conditions** if it's not already selected. - - :::image type="content" source="./media/debugging-with-visual-studio/breakpoint-settings-net6.png" alt-text="Editor showing breakpoint settings panel - C#"::: - -1. For the **Conditional Expression**, enter the following code in the field that shows example code that tests if `x` is 5. - - ```csharp - String.IsNullOrEmpty(name) - ``` - - ```vb - String.IsNullOrEmpty(name) - ``` - - 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`. - - Instead of a conditional expression, you can specify a *hit count*, which interrupts program execution before a statement is executed a specified number of times. Another option is to specify a *filter condition*, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name. - -1. Select **Close** to close the dialog. - -1. Start the program with debugging by pressing F5. - -1. In the console window, press the Enter key when prompted to enter your name. - -1. Because the condition you specified (`name` is either `null` or ) has been satisfied, program execution stops when it reaches the breakpoint and before the `Console.WriteLine` method executes. - -1. Select the **Locals** window, which shows the values of variables that are local to the currently executing method. In this case, `Main` is the currently executing method. Observe that the value of the `name` variable is `""`, or . - -1. Confirm the value is an empty string by entering the following statement in the **Immediate** window and pressing Enter. The result is `true`. - - ```csharp - ? name == String.Empty - ``` - - ```vb - ? String.IsNullOrEmpty(name) - ``` - - The question mark directs the immediate window to [evaluate an expression](/visualstudio/ide/reference/immediate-window#enter-commands). - - :::image type="content" source="./media/debugging-with-visual-studio/immediate-window-output.png" alt-text="Immediate Window returning a value of true after the statement is executed - C#"::: - -1. Press F5 to continue program execution. - -1. Press any key to close the console window and stop debugging. - -1. Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing **Debug > Toggle Breakpoint** while the line of code is selected. - -## Step through a program - -Visual Studio 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. While not currently debugging, and focus is on Visual Studio: - -1. Choose **Debug** > **Step Into**. Another way to debug one statement at a time is by pressing F11. - - Visual Studio highlights and displays an arrow beside the next line of execution. - - C# - - :::image type="content" source="./media/debugging-with-visual-studio/step-into-method-net6.png" alt-text="Visual Studio step into method - C#"::: - - Visual Basic - - :::image type="content" source="./media/debugging-with-visual-studio/vb-step-into-method.png" alt-text="Visual Studio step into method - Visual Basic"::: - - At this point, the **Locals** window shows that the `args` array is empty, and `name` and `currentDate` have default values. In addition, Visual Studio has opened a blank console window. - -1. Press F11. Visual Studio now highlights the next line of execution. The **Locals** window is unchanged, and the console window remains blank. - - C# - - :::image type="content" source="./media/debugging-with-visual-studio/step-into-source-method-net6.png" alt-text="Visual Studio step in method source - C#"::: - - Visual Basic - - :::image type="content" source="./media/debugging-with-visual-studio/vb-step-into-source-method.png" alt-text="Visual Studio step into method source - Visual Basic"::: - -1. Press F11. Visual Studio highlights the statement that includes the `name` variable assignment. The **Locals** window shows that `name` is `null`, and the console window displays the string "What is your name?". - -1. Respond to the prompt by entering a string in the console window and pressing Enter. The console is unresponsive, and the string you entered isn't displayed in the console window, but the method will nevertheless capture your input. - -1. Press F11. Visual Studio highlights the statement that includes the `currentDate` variable assignment. The **Locals** window shows the value returned by the call to the method. The console window also displays the string you entered at the prompt. - -1. Press F11. The **Locals** window shows the value of the `currentDate` variable after the assignment from the property. The console window is unchanged. - -1. Press F11. Visual Studio calls the method. The console window displays the formatted string. - -1. Choose **Debug** > **Step Out**. Another way to stop step-by-step execution is by pressing Shift+F11. - - The console window displays a message and waits for you to press a key. - -1. Press any key to close the console window and stop debugging. - -## Use Release build configuration - -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 sometimes negatively affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications. - -To build and test the Release version of your console application, change the build configuration on the toolbar from **Debug** to **Release**. - -:::image type="content" source="./media/debugging-with-visual-studio/visual-studio-toolbar-release.png" alt-text="default Visual Studio toolbar with release highlighted"::: - -When you press F5 or choose **Build Solution** from the **Build** menu, Visual Studio compiles the Release version of the application. You can test it as you did the Debug version. - -## Next steps - -In this tutorial, you used Visual Studio debugging tools. In the next tutorial, you publish a deployable version of the app. - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end diff --git a/docs/core/tutorials/library-with-visual-studio-code.md b/docs/core/tutorials/library-with-visual-studio-code.md index caee45d4d0943..aa9c4089bae8a 100644 --- a/docs/core/tutorials/library-with-visual-studio-code.md +++ b/docs/core/tutorials/library-with-visual-studio-code.md @@ -2,12 +2,9 @@ title: Create a .NET class library using Visual Studio Code description: Learn how to create a .NET class library using Visual Studio Code. ms.date: 09/12/2024 -zone_pivot_groups: dotnet-version --- # Tutorial: Create a .NET class library using Visual Studio Code -::: zone pivot="dotnet-9-0,dotnet-8-0" - In this tutorial, you create a simple utility library that contains a single string-handling method. A *class library* defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 8, it can be called by any application that targets .NET 8. This tutorial shows how to target .NET 8. @@ -149,271 +146,3 @@ In this tutorial, you created a library project and added a console app project > [!div class="nextstepaction"] > [Test a .NET class library with .NET using Visual Studio Code](testing-library-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -In this tutorial, you create a simple utility library that contains a single string-handling method. - -A *class library* defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 7, it can be called by any application that targets .NET 7. This tutorial shows how to target .NET 7. - -When you create a class library, you can distribute it as a third-party component or as a bundled component with one or more applications. - -## Prerequisites - -* [Visual Studio Code](https://code.visualstudio.com/) with [C# Dev Kit extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) installed. For information about how to install extensions on Visual Studio Code, see [VS Code Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-gallery). -* The [.NET 7 SDK](https://dotnet.microsoft.com/download/dotnet/7.0). - -## Create a class library project and solution - -Start by creating a .NET class library project named "StringLibrary" and an associated solution. A solution serves as a container for one or more projects. You'll add additional, related projects to the same solution. - -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. - -1. After selecting the command, you'll need to choose the project template. Choose Class Library. - -1. Then select the location where you would like the new project to be created. - -1. Give it the name **StringLibrary**, select **Show all template options**, select **.NET 7** and select **Create Project**. - -1. Check to make sure that the library targets .NET 7. In **Explorer**, open *StringLibrary/StringLibrary.csproj*. - - The `TargetFramework` element shows that the project targets .NET 7.0. - - ```xml - - - - net7.0 - - - - ``` - -1. Open *Class1.cs* and replace the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: - - The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. - - `StartsWithUpper` is implemented as an [extension method](../../csharp/programming-guide/classes-and-structs/extension-methods.md) so that you can call it as if it were a member of the class. - -1. Save the file. - -1. Right click on the project in **Solution Explorer** and right-click **Build** or open the Command Palette and select **.NET: Build** to build the solution and verify that the project compiles without error. - - The terminal output looks like the following example: - - ```output - Microsoft (R) Build Engine version 16.7.4+b89cb5fde for .NET - Copyright (C) Microsoft Corporation. All rights reserved. - Determining projects to restore... - All projects are up-to-date for restore. - StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net7.0\StringLibrary.dll - Build succeeded. - 0 Warning(s) - 0 Error(s) - Time Elapsed 00:00:02.78 - ``` - -## Add a console app to the solution - -Add a console application that uses the class library. The app will prompt the user to enter a string and report whether the string begins with an uppercase character. - -1. In the **Solution Explorer** select **New Project** or in the Command Palette select **.NET: New Project**. - -1. Select Console app. - -1. Give it the name **ShowCase**, select the default location and select **Create Project**. - -1. Open *ShowCase/Program.cs* and replace all of the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: - - The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. - - The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the Enter key without entering a string, the application ends, and the console window closes. - -1. Save your changes. - -## Add a project reference - -Initially, the new console app project doesn't have access to the class library. To allow it to call methods in the class library, create a project reference to the class library project. - -1. In **Solution Explorer** right click on the **ShowCase** project and select **Add Project Reference**. - -1. Select StringLibrary. - -## Run the app - -1. Select Run>Run without debugging. - -1. Select C#. - -1. Select ShowCase. - -1. Try out the program by entering strings and pressing Enter, then press Enter to exit. - - The terminal output looks like the following example: - - ```output - Press only to exit; otherwise, enter a string and press : - - A string that starts with an uppercase letter - Input: A string that starts with an uppercase letter - Begins with uppercase? : Yes - - a string that starts with a lowercase letter - Input: a string that starts with a lowercase letter - Begins with uppercase? : No - ``` - -## Additional resources - -* [Develop libraries with the .NET CLI](libraries.md) -* [.NET Standard versions and the platforms they support](../../standard/net-standard.md). - -## Next steps - -In this tutorial, you created a solution, added a library project, and added a console app project that uses the library. In the next tutorial, you add a unit test project to the solution. - -> [!div class="nextstepaction"] -> [Test a .NET class library with .NET using Visual Studio Code](testing-library-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -In this tutorial, you create a simple utility library that contains a single string-handling method. - -A *class library* defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 6, it can be called by any application that targets .NET 6. This tutorial shows how to target .NET 6. - -When you create a class library, you can distribute it as a third-party component or as a bundled component with one or more applications. - -## Prerequisites - -* [Visual Studio Code](https://code.visualstudio.com/) with [C# Dev Kit extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) installed. For information about how to install extensions on Visual Studio Code, see [VS Code Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-gallery). -* The [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0). - -## Create a class library project and solution - -Start by creating a .NET class library project named "StringLibrary" and an associated solution. A solution serves as a container for one or more projects. You'll add additional, related projects to the same solution. - -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. - -1. After selecting the command, you'll need to choose the project template. Choose Class Library. - -1. Then select the location where you would like the new project to be created. - -1. Give it the name **StringLibrary**, select **Show all template options**, select **.NET 7** and select **Create Project**. - -1. Check to make sure that the library targets .NET 6. In **Explorer**, open *StringLibrary/StringLibrary.csproj*. - - The `TargetFramework` element shows that the project targets .NET 6.0. - - ```xml - - - - net6.0 - - - - ``` - -1. Open *Class1.cs* and replace the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: - - The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. - - `StartsWithUpper` is implemented as an [extension method](../../csharp/programming-guide/classes-and-structs/extension-methods.md) so that you can call it as if it were a member of the class. - -1. Save the file. - -1. Right click on the project in **Solution Explorer** and right-click **Build** or open the Command Palette and select **.NET: Build** to build the solution and verify that the project compiles without error. - - The terminal output looks like the following example: - - ```output - Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET - Copyright (C) Microsoft Corporation. All rights reserved. - Determining projects to restore... - All projects are up-to-date for restore. - StringLibrary -> C:\Projects\ClassLibraryProjects\StringLibrary\bin\Debug\net6.0\StringLibrary.dll - Build succeeded. - 0 Warning(s) - 0 Error(s) - Time Elapsed 00:00:02.78 - ``` - -## Add a console app to the solution - -Add a console application that uses the class library. The app will prompt the user to enter a string and report whether the string begins with an uppercase character. - -1. In the **Solution Explorer** select **New Project** or in the Command Palette select **.NET: New Project**. - -1. Select Console app. - -1. Give it the name **ShowCase**, select the default location and select **Create Project**. - -1. Open *ShowCase/Program.cs* and replace all of the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: - - The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. - - The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the Enter key without entering a string, the application ends, and the console window closes. - -1. Save your changes. - -## Add a project reference - -Initially, the new console app project doesn't have access to the class library. To allow it to call methods in the class library, create a project reference to the class library project. - -1. In **Solution Explorer** right click on the **ShowCase** project and select **Add Project Reference**. - -1. Select StringLibrary. - -## Run the app - -1. Select Run>Run without debugging. - -1. Select C#. - -1. Select ShowCase. - -1. Try out the program by entering strings and pressing Enter, then press Enter to exit. - - The terminal output looks like the following example: - - ```output - Press only to exit; otherwise, enter a string and press : - - A string that starts with an uppercase letter - Input: A string that starts with an uppercase letter - Begins with uppercase? : Yes - - a string that starts with a lowercase letter - Input: a string that starts with a lowercase letter - Begins with uppercase? : No - ``` - -## Additional resources - -* [Develop libraries with the .NET CLI](libraries.md) -* [.NET Standard versions and the platforms they support](../../standard/net-standard.md). - -## Next steps - -In this tutorial, you created a solution, added a library project, and added a console app project that uses the library. In the next tutorial, you add a unit test project to the solution. - -> [!div class="nextstepaction"] -> [Test a .NET class library with .NET using Visual Studio Code](testing-library-with-visual-studio-code.md) - -::: zone-end diff --git a/docs/core/tutorials/library-with-visual-studio.md b/docs/core/tutorials/library-with-visual-studio.md index 754d51a1faad9..7a7c258871d05 100644 --- a/docs/core/tutorials/library-with-visual-studio.md +++ b/docs/core/tutorials/library-with-visual-studio.md @@ -2,7 +2,6 @@ title: Create a .NET class library using Visual Studio description: Learn how to create a .NET class library using Visual Studio. ms.date: 03/21/2024 -zone_pivot_groups: dotnet-version dev_langs: - "csharp" - "vb" @@ -10,8 +9,6 @@ ms.custom: vs-dotnet --- # Tutorial: Create a .NET class library using Visual Studio -::: zone pivot="dotnet-9-0,dotnet-8-0" - In this tutorial, you create a simple class library that contains a single string-handling method. A *class library* defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 8, it can be called by any application that targets .NET 8. This tutorial shows how to target .NET 8. @@ -141,273 +138,3 @@ Or learn how to publish a console app. If you publish the console app from the s > [!div class="nextstepaction"] > [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -In this tutorial, you create a simple class library that contains a single string-handling method. - -A *class library* defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 7, it can be called by any application that targets .NET 7. This tutorial shows how to target .NET 7. - -When you create a class library, you can distribute it as a NuGet package or as a component bundled with the application that uses it. - -## Prerequisites - -- [Visual Studio 2022 version 17.4](https://visualstudio.microsoft.com/downloads/?utm_medium=microsoft&utm_source=learn.microsoft.com&utm_campaign=inline+link&utm_content=download+vs2022) with the **.NET desktop development** workload installed. The .NET 7 SDK is automatically installed when you select this workload. - - For more information, see [Install the .NET SDK with Visual Studio](../install/windows.md#install-with-visual-studio). - -## Create a solution - -Start by creating a blank solution to put the class library project in. A Visual Studio solution serves as a container for one or more projects. You'll add additional, related projects to the same solution. - -To create the blank solution: - -1. Start Visual Studio. - -2. On the start window, choose **Create a new project**. - -3. On the **Create a new project** page, enter **solution** in the search box. Choose the **Blank Solution** template, and then choose **Next**. - - :::image type="content" source="media/library-with-visual-studio/blank-solution.png" alt-text="Blank solution template in Visual Studio"::: - -4. On the **Configure your new project** page, enter **ClassLibraryProjects** in the **Solution name** box. Then choose **Create**. - -## Create a class library project - -1. Add a new .NET class library project named "StringLibrary" to the solution. - - 1. Right-click on the solution in **Solution Explorer** and select **Add** > **New Project**. - - 1. On the **Add a new project** page, enter **library** in the search box. Choose **C#** or **Visual Basic** from the Language list, and then choose **All platforms** from the Platform list. Choose the **Class Library** template, and then choose **Next**. - - 1. On the **Configure your new project** page, enter **StringLibrary** in the **Project name** box, and then choose **Next**. - - 1. On the **Additional information** page, select **.NET 7 (Standard-term support)**, and then choose **Create**. - -1. Check to make sure that the library targets the correct version of .NET. Right-click on the library project in **Solution Explorer**, and then select **Properties**. The **Target Framework** text box shows that the project targets .NET 7.0. - -1. If you're using Visual Basic, clear the text in the **Root namespace** text box. - - :::image type="content" source="./media/library-with-visual-studio/vb/library-project-properties-net7.png" alt-text="Project properties for the class library"::: - - For each project, Visual Basic automatically creates a namespace that corresponds to the project name. In this tutorial, you define a top-level namespace by using the [`namespace`](../../visual-basic/language-reference/statements/namespace-statement.md) keyword in the code file. - -1. Replace the code in the code window for *Class1.cs* or *Class1.vb* with the following code, and save the file. If the language you want to use isn't shown, change the language selector at the top of the page. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: - :::code language="vb" source="./snippets/library-with-visual-studio/vb/StringLibrary/Class1.vb"::: - - The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. - - `StartsWithUpper` is implemented as an [extension method](../../csharp/programming-guide/classes-and-structs/extension-methods.md) so that you can call it as if it were a member of the class. The question mark (`?`) after `string` in the C# code indicates that the string may be null. - -1. On the menu bar, select **Build** > **Build Solution** or press Ctrl+Shift+B to verify that the project compiles without error. - -## Add a console app to the solution - -Add a console application that uses the class library. The app will prompt the user to enter a string and report whether the string begins with an uppercase character. - -1. Add a new .NET console application named "ShowCase" to the solution. - - 1. Right-click on the solution in **Solution Explorer** and select **Add** > **New project**. - - 1. On the **Add a new project** page, enter **console** in the search box. Choose **C#** or **Visual Basic** from the Language list, and then choose **All platforms** from the Platform list. - - 1. Choose the **Console Application** template, and then choose **Next**. - - 1. On the **Configure your new project** page, enter **ShowCase** in the **Project name** box. Then choose **Next**. - - 1. On the **Additional information** page, select **.NET 7 (Standard-term support)** in the **Framework** box. Then choose **Create**. - -1. In the code window for the *Program.cs* or *Program.vb* file, replace all of the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: - :::code language="vb" source="./snippets/library-with-visual-studio/vb/ShowCase/Program.vb"::: - - The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. - - The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the Enter key without entering a string, the application ends, and the console window closes. - -## Add a project reference - -Initially, the new console app project doesn't have access to the class library. To allow it to call methods in the class library, create a project reference to the class library project. - -1. In **Solution Explorer**, right-click the `ShowCase` project's **Dependencies** node, and select **Add Project Reference**. - - :::image type="content" source="media/library-with-visual-studio/add-reference-context-menu.png" alt-text="Add reference context menu in Visual Studio"::: - -1. In the **Reference Manager** dialog, select the **StringLibrary** project, and select **OK**. - - :::image type="content" source="media/library-with-visual-studio/manage-project-references.png" alt-text="Reference Manager dialog with StringLibrary selected"::: - -## Run the app - -1. In **Solution Explorer**, right-click the **ShowCase** project and select **Set as StartUp Project** in the context menu. - - :::image type="content" source="media/library-with-visual-studio/set-startup-project-context-menu.png" alt-text="Visual Studio project context menu to set startup project"::: - -1. Press Ctrl+F5 to compile and run the program without debugging. - -1. Try out the program by entering strings and pressing Enter, then press Enter to exit. - - :::image type="content" source="media/library-with-visual-studio/run-showcase-net6.png" alt-text="Console window with ShowCase running"::: - -## Additional resources - -- [Develop libraries with the .NET CLI](libraries.md) -- [.NET Standard versions and the platforms they support](../../standard/net-standard.md). - -## Next steps - -In this tutorial, you created a class library. In the next tutorial, you learn how to unit test the class library. - -> [!div class="nextstepaction"] -> [Unit test a .NET class library using Visual Studio](testing-library-with-visual-studio.md) - -Or you can skip automated unit testing and learn how to share the library by creating a NuGet package: - -> [!div class="nextstepaction"] -> [Create and publish a package using Visual Studio](/nuget/quickstart/create-and-publish-a-package-using-visual-studio) - -Or learn how to publish a console app. If you publish the console app from the solution you created in this tutorial, the class library goes with it as a *.dll* file. - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -In this tutorial, you create a simple class library that contains a single string-handling method. - -A *class library* defines types and methods that are called by an application. If the library targets .NET Standard 2.0, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 2.0. If the library targets .NET 6, it can be called by any application that targets .NET 6. This tutorial shows how to target .NET 6. - -When you create a class library, you can distribute it as a NuGet package or as a component bundled with the application that uses it. - -## Prerequisites - -- [Visual Studio 2022 version 17.0.0](https://visualstudio.microsoft.com/downloads/?utm_medium=microsoft&utm_source=learn.microsoft.com&utm_campaign=inline+link&utm_content=download+vs2022) with the **.NET desktop development** workload installed. The .NET 6 SDK is automatically installed when you select this workload. - - For more information, see [Install the .NET SDK with Visual Studio](../install/windows.md#install-with-visual-studio). - -## Create a solution - -Start by creating a blank solution to put the class library project in. A Visual Studio solution serves as a container for one or more projects. You'll add additional, related projects to the same solution. - -To create the blank solution: - -1. Start Visual Studio. - -2. On the start window, choose **Create a new project**. - -3. On the **Create a new project** page, enter **solution** in the search box. Choose the **Blank Solution** template, and then choose **Next**. - - :::image type="content" source="media/library-with-visual-studio/blank-solution.png" alt-text="Blank solution template in Visual Studio"::: - -4. On the **Configure your new project** page, enter **ClassLibraryProjects** in the **Solution name** box. Then choose **Create**. - -## Create a class library project - -1. Add a new .NET class library project named "StringLibrary" to the solution. - - 1. Right-click on the solution in **Solution Explorer** and select **Add** > **New Project**. - - 1. On the **Add a new project** page, enter **library** in the search box. Choose **C#** or **Visual Basic** from the Language list, and then choose **All platforms** from the Platform list. Choose the **Class Library** template, and then choose **Next**. - - 1. On the **Configure your new project** page, enter **StringLibrary** in the **Project name** box, and then choose **Next**. - - 1. On the **Additional information** page, select **.NET 6 (Long-term support)**, and then choose **Create**. - -1. Check to make sure that the library targets the correct version of .NET. Right-click on the library project in **Solution Explorer**, and then select **Properties**. The **Target Framework** text box shows that the project targets .NET 6.0. - -1. If you're using Visual Basic, clear the text in the **Root namespace** text box. - - :::image type="content" source="./media/library-with-visual-studio/vb/library-project-properties-net6.png" alt-text="Project properties for the class library"::: - - For each project, Visual Basic automatically creates a namespace that corresponds to the project name. In this tutorial, you define a top-level namespace by using the [`namespace`](../../visual-basic/language-reference/statements/namespace-statement.md) keyword in the code file. - -1. Replace the code in the code window for *Class1.cs* or *Class1.vb* with the following code, and save the file. If the language you want to use isn't shown, change the language selector at the top of the page. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: - :::code language="vb" source="./snippets/library-with-visual-studio/vb/StringLibrary/Class1.vb"::: - - The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. - - `StartsWithUpper` is implemented as an [extension method](../../csharp/programming-guide/classes-and-structs/extension-methods.md) so that you can call it as if it were a member of the class. The question mark (`?`) after `string` in the C# code indicates that the string may be null. - -1. On the menu bar, select **Build** > **Build Solution** or press Ctrl+Shift+B to verify that the project compiles without error. - -## Add a console app to the solution - -Add a console application that uses the class library. The app will prompt the user to enter a string and report whether the string begins with an uppercase character. - -1. Add a new .NET console application named "ShowCase" to the solution. - - 1. Right-click on the solution in **Solution Explorer** and select **Add** > **New project**. - - 1. On the **Add a new project** page, enter **console** in the search box. Choose **C#** or **Visual Basic** from the Language list, and then choose **All platforms** from the Platform list. - - 1. Choose the **Console Application** template, and then choose **Next**. - - 1. On the **Configure your new project** page, enter **ShowCase** in the **Project name** box. Then choose **Next**. - - 1. On the **Additional information** page, select **.NET 6 (Long-term support)** in the **Framework** box. Then choose **Create**. - -1. In the code window for the *Program.cs* or *Program.vb* file, replace all of the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: - :::code language="vb" source="./snippets/library-with-visual-studio/vb/ShowCase/Program.vb"::: - - The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. - - The program prompts the user to enter a string. It indicates whether the string starts with an uppercase character. If the user presses the Enter key without entering a string, the application ends, and the console window closes. - -## Add a project reference - -Initially, the new console app project doesn't have access to the class library. To allow it to call methods in the class library, create a project reference to the class library project. - -1. In **Solution Explorer**, right-click the `ShowCase` project's **Dependencies** node, and select **Add Project Reference**. - - :::image type="content" source="media/library-with-visual-studio/add-reference-context-menu.png" alt-text="Add reference context menu in Visual Studio"::: - -1. In the **Reference Manager** dialog, select the **StringLibrary** project, and select **OK**. - - :::image type="content" source="media/library-with-visual-studio/manage-project-references.png" alt-text="Reference Manager dialog with StringLibrary selected"::: - -## Run the app - -1. In **Solution Explorer**, right-click the **ShowCase** project and select **Set as StartUp Project** in the context menu. - - :::image type="content" source="media/library-with-visual-studio/set-startup-project-context-menu.png" alt-text="Visual Studio project context menu to set startup project"::: - -1. Press Ctrl+F5 to compile and run the program without debugging. - -1. Try out the program by entering strings and pressing Enter, then press Enter to exit. - - :::image type="content" source="media/library-with-visual-studio/run-showcase-net6.png" alt-text="Console window with ShowCase running"::: - -## Additional resources - -- [Develop libraries with the .NET CLI](libraries.md) -- [.NET Standard versions and the platforms they support](../../standard/net-standard.md). - -## Next steps - -In this tutorial, you created a class library. In the next tutorial, you learn how to unit test the class library. - -> [!div class="nextstepaction"] -> [Unit test a .NET class library using Visual Studio](testing-library-with-visual-studio.md) - -Or you can skip automated unit testing and learn how to share the library by creating a NuGet package: - -> [!div class="nextstepaction"] -> [Create and publish a package using Visual Studio](/nuget/quickstart/create-and-publish-a-package-using-visual-studio) - -Or learn how to publish a console app. If you publish the console app from the solution you created in this tutorial, the class library goes with it as a *.dll* file. - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end diff --git a/docs/core/tutorials/publishing-with-visual-studio-code.md b/docs/core/tutorials/publishing-with-visual-studio-code.md index 81b76692d5d0d..e724d0e997a16 100644 --- a/docs/core/tutorials/publishing-with-visual-studio-code.md +++ b/docs/core/tutorials/publishing-with-visual-studio-code.md @@ -2,12 +2,9 @@ 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 -zone_pivot_groups: dotnet-version --- # Tutorial: Publish a .NET console application using Visual Studio Code -::: zone pivot="dotnet-9-0,dotnet-8-0" - 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. @@ -112,223 +109,3 @@ In this tutorial, you published a console app. In the next tutorial, you create > [!div class="nextstepaction"] > [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -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. - -## 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 - -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). - -1. Choose **View** > **Terminal** from the main menu. - - The terminal opens in the *HelloWorld* folder. - -1. Run the following command: - - ```dotnetcli - dotnet publish --configuration Release - ``` - - The default build configuration is *Debug*, so this command specifies the *Release* build configuration. 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: - - ```output - Microsoft (R) Build Engine version 16.7.4+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\net7.0\HelloWorld.dll - HelloWorld -> C:\Projects\HelloWorld\bin\Release\net7.0\publish\ - ``` - -## Inspect the files - -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/net7.0/publish*. - - :::image type="content" source="media/publishing-with-visual-studio-code/published-files-output-net7.png" alt-text="Explorer showing published files"::: - - As the image shows, the published output includes the following files: - - - *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/deploy-with-cli.md#framework-dependent-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.exe* (*HelloWorld* on Linux, not created on macOS.) - - This is the [framework-dependent executable](../deploying/deploy-with-cli.md#framework-dependent-executable) version of the application. The file is operating-system-specific. - - - *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). - -## Run the published app - -1. In **Explorer**, right-click the *publish* folder (Ctrl-click on macOS), and select **Open in Terminal**. - - :::image type="content" source="media/publishing-with-visual-studio-code/open-in-terminal.png" alt-text="Context menu showing Open in Terminal"::: - -1. On Windows or Linux, run the app by using the executable. - - 1. On Windows, enter `.\HelloWorld.exe` and press Enter. On Windows with the Bash terminal, enter `./HelloWorld.exe`. - - 1. On Linux, enter `./HelloWorld` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -1. On any platform, run the app by using the [`dotnet`](../tools/dotnet.md) command: - - 1. Enter `dotnet HelloWorld.dll` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -## Additional resources - -- [.NET application deployment](../deploying/index.md) -- [Publish .NET apps with the .NET CLI](../deploying/deploy-with-cli.md) -- [`dotnet publish`](../tools/dotnet-publish.md) -- [Use the .NET SDK in continuous integration (CI) environments](../../devops/dotnet-cli-and-continuous-integration.md) - -## Next steps - -In this tutorial, you published a console app. In the next tutorial, you create a class library. - -> [!div class="nextstepaction"] -> [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -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. - -## 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 - -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). - -1. Choose **View** > **Terminal** from the main menu. - - The terminal opens in the *HelloWorld* folder. - -1. Run the following command: - - ```dotnetcli - dotnet publish --configuration Release - ``` - - The default build configuration is *Debug*, so this command specifies the *Release* build configuration. 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: - - ```output - Microsoft (R) Build Engine version 16.7.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\net6.0\HelloWorld.dll - HelloWorld -> C:\Projects\HelloWorld\bin\Release\net6.0\publish\ - ``` - -## Inspect the files - -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/net6.0/publish*. - - :::image type="content" source="media/publishing-with-visual-studio-code/published-files-output-net6.png" alt-text="Explorer showing published files"::: - - As the image shows, the published output includes the following files: - - - *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/deploy-with-cli.md#framework-dependent-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.exe* (*HelloWorld* on Linux, not created on macOS.) - - This is the [framework-dependent executable](../deploying/deploy-with-cli.md#framework-dependent-executable) version of the application. The file is operating-system-specific. - - - *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). - -## Run the published app - -1. In **Explorer**, right-click the *publish* folder (Ctrl-click on macOS), and select **Open in Terminal**. - - :::image type="content" source="media/publishing-with-visual-studio-code/open-in-terminal.png" alt-text="Context menu showing Open in Terminal"::: - -1. Run the app by using the executable. - - 1. On Windows, enter `.\HelloWorld.exe` and press Enter. - - 1. On Linux or macOS, enter `./HelloWorld` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -1. On any platform, run the app by using the [`dotnet`](../tools/dotnet.md) command: - - 1. Enter `dotnet HelloWorld.dll` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -## Additional resources - -- [.NET application deployment](../deploying/index.md) -- [Publish .NET apps with the .NET CLI](../deploying/deploy-with-cli.md) -- [`dotnet publish`](../tools/dotnet-publish.md) -- [Use the .NET SDK in continuous integration (CI) environments](../../devops/dotnet-cli-and-continuous-integration.md) - -## Next steps - -In this tutorial, you published a console app. In the next tutorial, you create a class library. - -> [!div class="nextstepaction"] -> [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md) - -::: zone-end diff --git a/docs/core/tutorials/publishing-with-visual-studio.md b/docs/core/tutorials/publishing-with-visual-studio.md index 34b37cb788da5..da30eb7f5058d 100644 --- a/docs/core/tutorials/publishing-with-visual-studio.md +++ b/docs/core/tutorials/publishing-with-visual-studio.md @@ -2,7 +2,6 @@ title: Publish a .NET console application using Visual Studio description: Learn how to use Visual Studio to create the set of files that are needed to run a .NET application. ms.date: 08/22/2023 -zone_pivot_groups: dotnet-version dev_langs: - "csharp" - "vb" @@ -10,8 +9,6 @@ ms.custom: "vs-dotnet" --- # Tutorial: Publish a .NET console application using Visual Studio -::: zone pivot="dotnet-9-0,dotnet-8-0" - 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 your application. To deploy the files, copy them to the target machine. ## Prerequisites @@ -120,229 +117,3 @@ In this tutorial, you published a console app. In the next tutorial, you create > [!div class="nextstepaction"] > [Create a .NET class library using Visual Studio](library-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -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 your application. To deploy the files, copy them to the target machine. - -## Prerequisites - -- This tutorial works with the console app that you create in [Create a .NET console application using Visual Studio](with-visual-studio.md). - -## Publish the app - -1. Start Visual Studio. - -1. Open the *HelloWorld* project that you created in [Create a .NET console application using Visual Studio](with-visual-studio.md). - -1. Make sure that Visual Studio is using the Release build configuration. If necessary, change the build configuration setting on the toolbar from **Debug** to **Release**. - - :::image type="content" source="media/publishing-with-visual-studio/visual-studio-toolbar-release.png" alt-text="Visual Studio toolbar with release build selected"::: - -1. Right-click on the **HelloWorld** project (not the HelloWorld solution) and select **Publish** from the menu. - - :::image type="content" source="media/publishing-with-visual-studio/publish-context-menu.png" alt-text="Visual Studio Publish context menu"::: - -1. On the **Target** tab of the **Publish** page, select **Folder**, and then select **Next**. - - :::image type="content" source="media/publishing-with-visual-studio/pick-publish-target.png" alt-text="Pick a publish target in Visual Studio"::: - -1. On the **Specific Target** tab of the **Publish** page, select **Folder**, and then select **Next**. - - :::image type="content" source="media/publishing-with-visual-studio/pick-specific-publish-target.png" alt-text="Pick the specific publish target in Visual Studio"::: - -1. On the **Location** tab of the **Publish** page, select **Finish**. - - :::image type="content" source="media/publishing-with-visual-studio/publish-page-loc-tab-net7.png" alt-text="Visual Studio Publish page Location tab"::: - -1. On the **Publish profile creation progress** page, select **Close**. - -1. On the **Publish** tab of the **Publish** window, select **Publish**. - - :::image type="content" source="media/publishing-with-visual-studio/publish-page-net7.png" alt-text="Visual Studio Publish window"::: - -## Inspect the files - -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. Users can run the published app by double-clicking the executable or issuing 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. In **Solution Explorer**, select **Show all files**. - -1. In the project folder, expand *bin/Release/net7.0/publish*. - - :::image type="content" source="media/publishing-with-visual-studio/published-files-output-net7.png" alt-text="Solution Explorer showing published files"::: - - As the image shows, the published output includes the following files: - - - *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/85ca206d84633d658d7363894c4ea9d59e515c1a/Documentation/specs/runtime-configuration-file.md). - - - *HelloWorld.dll* - - This is the [framework-dependent deployment](../deploying/deploy-with-cli.md#framework-dependent-deployment) version of the application. To execute 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.exe* - - This is the [framework-dependent executable](../deploying/deploy-with-cli.md#framework-dependent-executable) version of the application. To run it, enter `HelloWorld.exe` at a command prompt. The file is operating-system-specific. - - - *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). - -## Run the published app - -1. In **Solution Explorer**, right-click the *publish* folder, and select **Copy Full Path**. - -1. Open a command prompt and navigate to the *publish* folder. To do that, enter `cd` and then paste the full path. For example: - - ```console - cd C:\Projects\HelloWorld\bin\Release\net7.0\publish\ - ``` - -1. Run the app by using the executable: - - 1. Enter `HelloWorld.exe` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -1. Run the app by using the `dotnet` command: - - 1. Enter `dotnet HelloWorld.dll` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -## Additional resources - -- [.NET application deployment](../deploying/index.md) -- [Publish .NET apps with the .NET CLI](../deploying/deploy-with-cli.md) -- [`dotnet publish`](../tools/dotnet-publish.md) -- [Tutorial: Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) -- [Use the .NET SDK in continuous integration (CI) environments](../../devops/dotnet-cli-and-continuous-integration.md) - -## Next steps - -In this tutorial, you published a console app. In the next tutorial, you create a class library. - -> [!div class="nextstepaction"] -> [Create a .NET class library using Visual Studio](library-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -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 your application. To deploy the files, copy them to the target machine. - -## Prerequisites - -- This tutorial works with the console app that you create in [Create a .NET console application using Visual Studio](with-visual-studio.md). - -## Publish the app - -1. Start Visual Studio. - -1. Open the *HelloWorld* project that you created in [Create a .NET console application using Visual Studio](with-visual-studio.md). - -1. Make sure that Visual Studio is using the Release build configuration. If necessary, change the build configuration setting on the toolbar from **Debug** to **Release**. - - :::image type="content" source="media/publishing-with-visual-studio/visual-studio-toolbar-release.png" alt-text="Visual Studio toolbar with release build selected"::: - -1. Right-click on the **HelloWorld** project (not the HelloWorld solution) and select **Publish** from the menu. - - :::image type="content" source="media/publishing-with-visual-studio/publish-context-menu.png" alt-text="Visual Studio Publish context menu"::: - -1. On the **Target** tab of the **Publish** page, select **Folder**, and then select **Next**. - - :::image type="content" source="media/publishing-with-visual-studio/pick-publish-target.png" alt-text="Pick a publish target in Visual Studio"::: - -1. On the **Specific Target** tab of the **Publish** page, select **Folder**, and then select **Next**. - - :::image type="content" source="media/publishing-with-visual-studio/pick-specific-publish-target.png" alt-text="Pick the specific publish target in Visual Studio"::: - -1. On the **Location** tab of the **Publish** page, select **Finish**. - - :::image type="content" source="media/publishing-with-visual-studio/publish-page-loc-tab-net6.png" alt-text="Visual Studio Publish page Location tab"::: - -1. On the **Publish** tab of the **Publish** window, select **Publish**. - - :::image type="content" source="media/publishing-with-visual-studio/publish-page-net6.png" alt-text="Visual Studio Publish window"::: - -## Inspect the files - -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. Users can run the published app by double-clicking the executable or issuing 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. In **Solution Explorer**, select **Show all files**. - -1. In the project folder, expand *bin/Release/net6.0/publish*. - - :::image type="content" source="media/publishing-with-visual-studio/published-files-output-net6.png" alt-text="Solution Explorer showing published files"::: - - As the image shows, the published output includes the following files: - - - *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/85ca206d84633d658d7363894c4ea9d59e515c1a/Documentation/specs/runtime-configuration-file.md). - - - *HelloWorld.dll* - - This is the [framework-dependent deployment](../deploying/deploy-with-cli.md#framework-dependent-deployment) version of the application. To execute 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.exe* - - This is the [framework-dependent executable](../deploying/deploy-with-cli.md#framework-dependent-executable) version of the application. To run it, enter `HelloWorld.exe` at a command prompt. The file is operating-system-specific. - - - *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). - -## Run the published app - -1. In **Solution Explorer**, right-click the *publish* folder, and select **Copy Full Path**. - -1. Open a command prompt and navigate to the *publish* folder. To do that, enter `cd` and then paste the full path. For example: - - ```console - cd C:\Projects\HelloWorld\bin\Release\net6.0\publish\ - ``` - -1. Run the app by using the executable: - - 1. Enter `HelloWorld.exe` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -1. Run the app by using the `dotnet` command: - - 1. Enter `dotnet HelloWorld.dll` and press Enter. - - 1. Enter a name in response to the prompt, and press any key to exit. - -## Additional resources - -- [.NET application deployment](../deploying/index.md) -- [Publish .NET apps with the .NET CLI](../deploying/deploy-with-cli.md) -- [`dotnet publish`](../tools/dotnet-publish.md) -- [Tutorial: Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) -- [Use the .NET SDK in continuous integration (CI) environments](../../devops/dotnet-cli-and-continuous-integration.md) - -## Next steps - -In this tutorial, you published a console app. In the next tutorial, you create a class library. - -> [!div class="nextstepaction"] -> [Create a .NET class library using Visual Studio](library-with-visual-studio.md) - -::: zone-end diff --git a/docs/core/tutorials/testing-library-with-visual-studio-code.md b/docs/core/tutorials/testing-library-with-visual-studio-code.md index 07bcdbe53df41..155049cb65641 100644 --- a/docs/core/tutorials/testing-library-with-visual-studio-code.md +++ b/docs/core/tutorials/testing-library-with-visual-studio-code.md @@ -2,12 +2,9 @@ title: Test a .NET class library using Visual Studio Code description: Learn how to use Visual Studio Code and run a unit test project for a .NET class library. ms.date: 06/14/2024 -zone_pivot_groups: dotnet-version --- # Tutorial: Test a .NET class library using Visual Studio Code -::: zone pivot="dotnet-9-0,dotnet-8-0" - This tutorial shows how to automate unit testing by adding a test project to a solution. ## Prerequisites @@ -160,311 +157,3 @@ The Visual Studio Code extension C# Dev Kit provides more tools for developing C > [!div class="nextstepaction"] > [C# Dev Kit](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp-dev-kit) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -This tutorial shows how to automate unit testing by adding a test project to a solution. - -## Prerequisites - -- This tutorial works with the solution that you create in [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md). - -## Create a unit test project - -Unit tests provide automated software testing during your development and publishing. The testing framework that you use in this tutorial is MSTest. [MSTest](https://github.com/Microsoft/testfx-docs) is one of three test frameworks you can choose from. The others are [xUnit](https://xunit.net/) and [nUnit](https://nunit.org/). - -1. Start Visual Studio Code. - -1. Open the `ClassLibraryProjects` solution you created in [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md). - -1. From **Solution Explorer**, select **Add Project**, or from the Command Palette select **.NET: New Project**. - -1. Select **MSTest Test Project**, name it "StringLibraryTest", select the default directory, and select **Create Project**. - - The project template creates a UnitTest1.cs file with the following code: - - ```csharp - using Microsoft.VisualStudio.TestTools.UnitTesting; - - namespace StringLibraryTest - { - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestMethod1() - { - } - } - } - ``` - - The source code created by the unit test template does the following: - - - It imports the namespace, which contains the types used for unit testing. - - It applies the attribute to the `UnitTest1` class. - - It applies the attribute to define `TestMethod1`. - - Each method tagged with [[TestMethod]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute) in a test class tagged with [[TestClass]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute) is run automatically when the unit test is invoked. - -## Add a project reference - -For the test project to work with the `StringLibrary` class, add a reference in the `StringLibraryTest` project to the `StringLibrary` project. - -1. From **Solution Explorer** right click on the 'StringLibraryTest' Project and select **Add Project Reference**. - -1. Select "StringLibrary". - -## Add and run unit test methods - -When Visual Studio invokes a unit test, it runs each method that is marked with the attribute in a class that is marked with the attribute. A test method ends when the first failure is found or when all tests contained in the method have succeeded. - -The most common tests call members of the class. Many assert methods include at least two parameters, one of which is the expected test result and the other of which is the actual test result. Some of the `Assert` class's most frequently called methods are shown in the following table: - -| Assert methods | Function | -| ------------------ | -------- | -| `Assert.AreEqual` | Verifies that two values or objects are equal. The assert fails if the values or objects aren't equal. | -| `Assert.AreSame` | Verifies that two object variables refer to the same object. The assert fails if the variables refer to different objects. | -| `Assert.IsFalse` | Verifies that a condition is `false`. The assert fails if the condition is `true`. | -| `Assert.IsNotNull` | Verifies that an object isn't `null`. The assert fails if the object is `null`. | - -You can also use the method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown. - -In testing the `StringLibrary.StartsWithUpper` method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return `true` in these cases, so you can call the method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return `false` in these cases, so you can call the method. - -Since your library method handles strings, you also want to make sure that it successfully handles an [empty string (`String.Empty`)](xref:System.String.Empty) and a `null` string. An empty string is one that has no characters and whose is 0. A `null` string is one that hasn't been initialized. You can call `StartsWithUpper` directly as a static method and pass a single argument. Or you can call `StartsWithUpper` as an extension method on a `string` variable assigned to `null`. - -You'll define three methods, each of which calls an method for each element in a string array. You'll call a method overload that lets you specify an error message to be displayed in case of test failure. The message identifies the string that caused the failure. - -To create the test methods: - -1. Open *StringLibraryTest/UnitTest1.cs* and replace all of the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs"::: - - The test of uppercase characters in the `TestStartsWithUpper` method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the `TestDoesNotStartWithUpper` method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). - -1. Save your changes. - -## Build and Run your Tests - -1. In **Solution Explorer**, right-click and select **Build** or from the Command Palette, select **.NET: Build**. - -1. Select the **Testing** window, select **Run Tests** or from the Command Palette, select **Test: Run all Tests**. - - :::image type="content" source="media/testing-library-with-visual-studio-code/testingScreenshot.png" alt-text="Visual Studio Code Test Explorer"::: - -## Handle test failures - -If you're doing test-driven development (TDD), you write tests first and they fail the first time you run them. Then you add code to the app that makes the test succeed. For this tutorial, you created the test after writing the app code that it validates, so you haven't seen the test fail. To validate that a test fails when you expect it to fail, add an invalid value to the test input. - -1. Modify the `words` array in the `TestDoesNotStartWithUpper` method to include the string "Error". - - ```csharp - string[] words = { "alphabet", "Error", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " }; - ``` - -1. Run the tests by clicking on the green error next to the test in the editor. - - The output shows that the test fails, and it provides an error message for the failed test: "Assert.IsFalse failed. Expected for 'Error': false; actual: True". Because of the failure, no strings in the array after "Error" were tested. - - :::image type="content" source="media/testing-library-with-visual-studio-code/failedTest.png" alt-text="Visual Studio Code Failed Test"::: - -1. Remove the string "Error" that you added in step 1. Rerun the test and the tests pass. - -## Test the Release version of the library - -Now that the tests have all passed when running the Debug build of the library, run the tests an additional time against the Release build of the library. A number of factors, including compiler optimizations, can sometimes produce different behavior between Debug and Release builds. - -1. Run the tests with the Release build configuration: - - ```dotnetcli - dotnet test StringLibraryTest/StringLibraryTest.csproj --configuration Release - ``` - - The tests pass. - -## Debug tests - -If you're using Visual Studio Code as your IDE, you can use the same process shown in [Debug a .NET console application using Visual Studio Code](debugging-with-visual-studio-code.md) to debug code using your unit test project. Instead of starting the *ShowCase* app project, open *StringLibraryTest/UnitTest1.cs*, and select **Debug Tests in current file** between lines 7 and 8. If you're unable to find it, press Ctrl+Shift+P to open the command palette and enter **Reload Window**. - -Visual Studio Code starts the test project with the debugger attached. Execution will stop at any breakpoint you've added to the test project or the underlying library code. - -## Additional resources - -- [Unit testing in .NET](../testing/index.md) - -## Next steps - -In this tutorial, you unit tested a class library. You can make the library available to others by publishing it to [NuGet](https://nuget.org) as a package. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Create and publish a package using the dotnet CLI](/nuget/quickstart/create-and-publish-a-package-using-the-dotnet-cli) - -If you publish a library as a NuGet package, others can install and use it. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Install and use a package using the dotnet CLI](/nuget/quickstart/install-and-use-a-package-using-the-dotnet-cli) - -A library doesn't have to be distributed as a package. It can be bundled with a console app that uses it. To learn how to publish a console app, see the earlier tutorial in this series: - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -This tutorial shows how to automate unit testing by adding a test project to a solution. - -## Prerequisites - -- This tutorial works with the solution that you create in [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md). - -## Create a unit test project - -Unit tests provide automated software testing during your development and publishing. The testing framework that you use in this tutorial is MSTest. [MSTest](https://github.com/Microsoft/testfx-docs) is one of three test frameworks you can choose from. The others are [xUnit](https://xunit.net/) and [nUnit](https://nunit.org/). - -1. Start Visual Studio Code. - -1. Open the `ClassLibraryProjects` solution you created in [Create a .NET class library using Visual Studio Code](library-with-visual-studio-code.md). - -1. From **Solution Explorer**, select **Add Project**, or from the Command Palette select **.NET: New Project**. - -1. Select **MSTest Test Project**, name it "StringLibraryTest", select the default directory, and select **Create Project**. - - The project template creates a UnitTest1.cs file with the following code: - - ```csharp - using Microsoft.VisualStudio.TestTools.UnitTesting; - - namespace StringLibraryTest - { - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestMethod1() - { - } - } - } - ``` - - The source code created by the unit test template does the following: - - - It imports the namespace, which contains the types used for unit testing. - - It applies the attribute to the `UnitTest1` class. - - It applies the attribute to define `TestMethod1`. - - Each method tagged with [[TestMethod]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute) in a test class tagged with [[TestClass]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute) is run automatically when the unit test is invoked. - -## Add a project reference - -For the test project to work with the `StringLibrary` class, add a reference in the `StringLibraryTest` project to the `StringLibrary` project. - -1. From **Solution Explorer** right click on the 'StringLibraryTest' Project and select **Add Project Reference**. - -1. Select "StringLibrary". - -## Add and run unit test methods - -When Visual Studio invokes a unit test, it runs each method that is marked with the attribute in a class that is marked with the attribute. A test method ends when the first failure is found or when all tests contained in the method have succeeded. - -The most common tests call members of the class. Many assert methods include at least two parameters, one of which is the expected test result and the other of which is the actual test result. Some of the `Assert` class's most frequently called methods are shown in the following table: - -| Assert methods | Function | -| ------------------ | -------- | -| `Assert.AreEqual` | Verifies that two values or objects are equal. The assert fails if the values or objects aren't equal. | -| `Assert.AreSame` | Verifies that two object variables refer to the same object. The assert fails if the variables refer to different objects. | -| `Assert.IsFalse` | Verifies that a condition is `false`. The assert fails if the condition is `true`. | -| `Assert.IsNotNull` | Verifies that an object isn't `null`. The assert fails if the object is `null`. | - -You can also use the method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown. - -In testing the `StringLibrary.StartsWithUpper` method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return `true` in these cases, so you can call the method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return `false` in these cases, so you can call the method. - -Since your library method handles strings, you also want to make sure that it successfully handles an [empty string (`String.Empty`)](xref:System.String.Empty) and a `null` string. An empty string is one that has no characters and whose is 0. A `null` string is one that hasn't been initialized. You can call `StartsWithUpper` directly as a static method and pass a single argument. Or you can call `StartsWithUpper` as an extension method on a `string` variable assigned to `null`. - -You'll define three methods, each of which calls an method for each element in a string array. You'll call a method overload that lets you specify an error message to be displayed in case of test failure. The message identifies the string that caused the failure. - -To create the test methods: - -1. Open *StringLibraryTest/UnitTest1.cs* and replace all of the code with the following code. - - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs"::: - - The test of uppercase characters in the `TestStartsWithUpper` method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the `TestDoesNotStartWithUpper` method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). - -1. Save your changes. - -## Build and Run your Tests - -1. In **Solution Explorer**, right-click and select **Build** or from the Command Palette, select **.NET: Build**. - -1. Select the **Testing** window, select **Run Tests** or from the Command Palette, select **Test: Run all Tests**. - - :::image type="content" source="media/testing-library-with-visual-studio-code/testingScreenshot.png" alt-text="Visual Studio Code Test Explorer"::: - -## Handle test failures - -If you're doing test-driven development (TDD), you write tests first and they fail the first time you run them. Then you add code to the app that makes the test succeed. For this tutorial, you created the test after writing the app code that it validates, so you haven't seen the test fail. To validate that a test fails when you expect it to fail, add an invalid value to the test input. - -1. Modify the `words` array in the `TestDoesNotStartWithUpper` method to include the string "Error". - - ```csharp - string[] words = { "alphabet", "Error", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " }; - ``` - -1. Run the tests by clicking on the green error next to the test in the editor. - - The output shows that the test fails, and it provides an error message for the failed test: "Assert.IsFalse failed. Expected for 'Error': false; actual: True". Because of the failure, no strings in the array after "Error" were tested. - - :::image type="content" source="media/testing-library-with-visual-studio-code/failedTest.png" alt-text="Visual Studio Code Failed Test"::: - -1. Remove the string "Error" that you added in step 1. Rerun the test and the tests pass. - -## Test the Release version of the library - -Now that the tests have all passed when running the Debug build of the library, run the tests an additional time against the Release build of the library. A number of factors, including compiler optimizations, can sometimes produce different behavior between Debug and Release builds. - -1. Run the tests with the Release build configuration: - - ```dotnetcli - dotnet test StringLibraryTest/StringLibraryTest.csproj --configuration Release - ``` - - The tests pass. - -## Debug tests - -If you're using Visual Studio Code as your IDE, you can use the same process shown in [Debug a .NET console application using Visual Studio Code](debugging-with-visual-studio-code.md) to debug code using your unit test project. Instead of starting the *ShowCase* app project, open *StringLibraryTest/UnitTest1.cs*, and select **Debug Tests in current file** between lines 7 and 8. If you're unable to find it, press Ctrl+Shift+P to open the command palette and enter **Reload Window**. - -Visual Studio Code starts the test project with the debugger attached. Execution will stop at any breakpoint you've added to the test project or the underlying library code. - -## Additional resources - -- [Unit testing in .NET](../testing/index.md) - -## Next steps - -In this tutorial, you unit tested a class library. You can make the library available to others by publishing it to [NuGet](https://nuget.org) as a package. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Create and publish a package using the dotnet CLI](/nuget/quickstart/create-and-publish-a-package-using-the-dotnet-cli) - -If you publish a library as a NuGet package, others can install and use it. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Install and use a package using the dotnet CLI](/nuget/quickstart/install-and-use-a-package-using-the-dotnet-cli) - -A library doesn't have to be distributed as a package. It can be bundled with a console app that uses it. To learn how to publish a console app, see the earlier tutorial in this series: - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio Code](publishing-with-visual-studio-code.md) - -::: zone-end diff --git a/docs/core/tutorials/testing-library-with-visual-studio.md b/docs/core/tutorials/testing-library-with-visual-studio.md index b0977c9720a7e..c5254af5f5a2b 100644 --- a/docs/core/tutorials/testing-library-with-visual-studio.md +++ b/docs/core/tutorials/testing-library-with-visual-studio.md @@ -2,7 +2,6 @@ title: Test a .NET class library using Visual Studio description: Learn how to use Visual Studio to create and run a unit test project for a .NET class library. ms.date: 08/23/2023 -zone_pivot_groups: dotnet-version dev_langs: - "csharp" - "vb" @@ -10,8 +9,6 @@ ms.custom: "vs-dotnet" --- # Tutorial: Test a .NET class library with .NET using Visual Studio -::: zone pivot="dotnet-9-0,dotnet-8-0" - This tutorial shows how to automate unit testing by adding a test project to a solution. ## Prerequisites @@ -203,403 +200,3 @@ A library doesn't have to be distributed as a package. It can be bundled with a > [!div class="nextstepaction"] > [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -This tutorial shows how to automate unit testing by adding a test project to a solution. - -## Prerequisites - -- This tutorial works with the solution that you create in [Create a .NET class library using Visual Studio](library-with-visual-studio.md). - -## Create a unit test project - -Unit tests provide automated software testing during your development and publishing. [MSTest](https://github.com/Microsoft/testfx-docs) is one of three test frameworks you can choose from. The others are [xUnit](https://xunit.net/) and [nUnit](https://nunit.org/). - -1. Start Visual Studio. - -1. Open the `ClassLibraryProjects` solution you created in [Create a .NET class library using Visual Studio](library-with-visual-studio.md). - -1. Add a new unit test project named "StringLibraryTest" to the solution. - - 1. Right-click on the solution in **Solution Explorer** and select **Add** > **New project**. - - 1. On the **Add a new project** page, enter **mstest** in the search box. Choose **C#** or **Visual Basic** from the Language list, and then choose **All platforms** from the Platform list. - - 1. Choose the **MSTest Test Project** template, and then choose **Next**. - - 1. On the **Configure your new project** page, enter **StringLibraryTest** in the **Project name** box. Then choose **Next**. - - 1. On the **Additional information** page, select **.NET 7 (Standard-term support)** in the **Framework** box. Then choose **Create**. - -1. Visual Studio creates the project and opens the class file in the code window with the following code. If the language you want to use is not shown, change the language selector at the top of the page. - - ```csharp - using Microsoft.VisualStudio.TestTools.UnitTesting; - - namespace StringLibraryTest - { - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestMethod1() - { - } - } - } - ``` - - ```vb - Imports Microsoft.VisualStudio.TestTools.UnitTesting - - Namespace StringLibraryTest - - Public Class UnitTest1 - - Sub TestSub() - - End Sub - End Class - End Namespace - ``` - - The source code created by the unit test template does the following: - - - It imports the namespace, which contains the types used for unit testing. - - It applies the attribute to the `UnitTest1` class. - - It applies the attribute to define `TestMethod1` in C# or `TestSub` in Visual Basic. - - Each method tagged with [[TestMethod]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute) in a test class tagged with [[TestClass]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute) is executed automatically when the unit test is run. - -## Add a project reference - -For the test project to work with the `StringLibrary` class, add a reference in the **StringLibraryTest** project to the `StringLibrary` project. - -1. In **Solution Explorer**, right-click the **Dependencies** node of the **StringLibraryTest** project and select **Add Project Reference** from the context menu. - -1. In the **Reference Manager** dialog, expand the **Projects** node, and select the box next to **StringLibrary**. Adding a reference to the `StringLibrary` assembly allows the compiler to find **StringLibrary** methods while compiling the **StringLibraryTest** project. - -1. Select **OK**. - -## Add and run unit test methods - -When Visual Studio runs a unit test, it executes each method that is marked with the attribute in a class that is marked with the attribute. A test method ends when the first failure is found or when all tests contained in the method have succeeded. - -The most common tests call members of the class. Many assert methods include at least two parameters, one of which is the expected test result and the other of which is the actual test result. Some of the `Assert` class's most frequently called methods are shown in the following table: - -| Assert methods | Function | -| ------------------ | -------- | -| `Assert.AreEqual` | Verifies that two values or objects are equal. The assert fails if the values or objects aren't equal. | -| `Assert.AreSame` | Verifies that two object variables refer to the same object. The assert fails if the variables refer to different objects. | -| `Assert.IsFalse` | Verifies that a condition is `false`. The assert fails if the condition is `true`. | -| `Assert.IsNotNull` | Verifies that an object isn't `null`. The assert fails if the object is `null`. | - -You can also use the method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown. - -In testing the `StringLibrary.StartsWithUpper` method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return `true` in these cases, so you can call the method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return `false` in these cases, so you can call the method. - -Since your library method handles strings, you also want to make sure that it successfully handles an [empty string (`String.Empty`)](xref:System.String.Empty), a valid string that has no characters and whose is 0, and a `null` string that hasn't been initialized. You can call `StartsWithUpper` directly as a static method and pass a single argument. Or you can call `StartsWithUpper` as an extension method on a `string` variable assigned to `null`. - -You'll define three methods, each of which calls an method for each element in a string array. You'll call a method overload that lets you specify an error message to be displayed in case of test failure. The message identifies the string that caused the failure. - -To create the test methods: - -1. In the *UnitTest1.cs* or *UnitTest1.vb* code window, replace the code with the following code: - - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibraryTest/UnitTest1.cs"::: - :::code language="vb" source="./snippets/library-with-visual-studio/vb/StringLibraryTest/UnitTest1.vb"::: - - The test of uppercase characters in the `TestStartsWithUpper` method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the `TestDoesNotStartWithUpper` method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). - -1. On the menu bar, select **File** > **Save UnitTest1.cs As** or **File** > **Save UnitTest1.vb As**. In the **Save File As** dialog, select the arrow beside the **Save** button, and select **Save with Encoding**. - - :::image type="content" source="./media/testing-library-with-visual-studio/save-file-as-dialog.png" alt-text="Visual Studio Save File As dialog"::: - -1. In the **Confirm Save As** dialog, select the **Yes** button to save the file. - -1. In the **Advanced Save Options** dialog, select **Unicode (UTF-8 with signature) - Codepage 65001** from the **Encoding** drop-down list and select **OK**. - - :::image type="content" source="./media/testing-library-with-visual-studio/advanced-save-options.png" alt-text="Visual Studio Advanced Save Options dialog"::: - - If you fail to save your source code as a UTF8-encoded file, Visual Studio may save it as an ASCII file. When that happens, the runtime doesn't accurately decode the UTF8 characters outside of the ASCII range, and the test results won't be correct. - -1. On the menu bar, select **Test** > **Run All Tests**. If the **Test Explorer** window doesn't open, open it by choosing **Test** > **Test Explorer**. The three tests are listed in the **Passed Tests** section, and the **Summary** section reports the result of the test run. - - :::image type="content" source="./media/testing-library-with-visual-studio/test-explorer-window.png" alt-text="Test Explorer window with passing tests"::: - -## Handle test failures - -If you're doing test-driven development (TDD), you write tests first and they fail the first time you run them. Then you add code to the app that makes the test succeed. For this tutorial, you created the test after writing the app code that it validates, so you haven't seen the test fail. To validate that a test fails when you expect it to fail, add an invalid value to the test input. - -1. Modify the `words` array in the `TestDoesNotStartWithUpper` method to include the string "Error". You don't need to save the file because Visual Studio automatically saves open files when a solution is built to run tests. - - ```csharp - string[] words = { "alphabet", "Error", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " }; - ``` - - ```vb - Dim words() As String = { "alphabet", "Error", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " } - - ``` - -1. Run the test by selecting **Test** > **Run All Tests** from the menu bar. The **Test Explorer** window indicates that two tests succeeded and one failed. - - :::image type="content" source="./media/testing-library-with-visual-studio/failed-test-window.png" alt-text="Test Explorer window with failing tests"::: - -1. Select the failed test, `TestDoesNotStartWith`. - - The **Test Explorer** window displays the message produced by the assert: "Assert.IsFalse failed. Expected for 'Error': false; actual: True". Because of the failure, no strings in the array after "Error" were tested. - - :::image type="content" source="./media/testing-library-with-visual-studio/failed-test-detail.png" alt-text="Test Explorer window showing the IsFalse assertion failure"::: - -1. Remove the string "Error" that you added in step 1. Rerun the test and the tests pass. - -## Test the Release version of the library - -Now that the tests have all passed when running the Debug build of the library, run the tests an additional time against the Release build of the library. A number of factors, including compiler optimizations, can sometimes produce different behavior between Debug and Release builds. - -To test the Release build: - -1. In the Visual Studio toolbar, change the build configuration from **Debug** to **Release**. - - :::image type="content" source="./media/testing-library-with-visual-studio/visual-studio-toolbar-release.png" alt-text="Visual Studio toolbar with release build highlighted"::: - -1. In **Solution Explorer**, right-click the **StringLibrary** project and select **Build** from the context menu to recompile the library. - - :::image type="content" source="./media/testing-library-with-visual-studio/build-library-context-menu.png" alt-text="StringLibrary context menu with build command"::: - -1. Run the unit tests by choosing **Test** > **Run All Tests** from the menu bar. The tests pass. - -## Debug tests - -If you're using Visual Studio as your IDE, you can use the same process shown in [Tutorial: Debug a .NET console application using Visual Studio](debugging-with-visual-studio.md) to debug code using your unit test project. Instead of starting the *ShowCase* app project, right-click the **StringLibraryTests** project, and select **Debug Tests** from the context menu. - -Visual Studio starts the test project with the debugger attached. Execution will stop at any breakpoint you've added to the test project or the underlying library code. - -## Additional resources - -- [Unit test basics - Visual Studio](/visualstudio/test/unit-test-basics) -- [Unit testing in .NET](../testing/index.md) - -## Next steps - -In this tutorial, you unit tested a class library. You can make the library available to others by publishing it to [NuGet](https://nuget.org) as a package. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Create and publish a NuGet package using Visual Studio](/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli) - -If you publish a library as a NuGet package, others can install and use it. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Install and use a package in Visual Studio](/nuget/quickstart/install-and-use-a-package-in-visual-studio) - -A library doesn't have to be distributed as a package. It can be bundled with a console app that uses it. To learn how to publish a console app, see the earlier tutorial in this series: - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -This tutorial shows how to automate unit testing by adding a test project to a solution. - -## Prerequisites - -- This tutorial works with the solution that you create in [Create a .NET class library using Visual Studio](library-with-visual-studio.md). - -## Create a unit test project - -Unit tests provide automated software testing during your development and publishing. [MSTest](https://github.com/Microsoft/testfx-docs) is one of three test frameworks you can choose from. The others are [xUnit](https://xunit.net/) and [nUnit](https://nunit.org/). - -1. Start Visual Studio. - -1. Open the `ClassLibraryProjects` solution you created in [Create a .NET class library using Visual Studio](library-with-visual-studio.md). - -1. Add a new unit test project named "StringLibraryTest" to the solution. - - 1. Right-click on the solution in **Solution Explorer** and select **Add** > **New project**. - - 1. On the **Add a new project** page, enter **mstest** in the search box. Choose **C#** or **Visual Basic** from the Language list, and then choose **All platforms** from the Platform list. - - 1. Choose the **MSTest Test Project** template, and then choose **Next**. - - 1. On the **Configure your new project** page, enter **StringLibraryTest** in the **Project name** box. Then choose **Next**. - - 1. On the **Additional information** page, select **.NET 6 (Long-term support)** in the **Framework** box. Then choose **Create**. - -1. Visual Studio creates the project and opens the class file in the code window with the following code. If the language you want to use is not shown, change the language selector at the top of the page. - - ```csharp - using Microsoft.VisualStudio.TestTools.UnitTesting; - - namespace StringLibraryTest - { - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestMethod1() - { - } - } - } - ``` - - ```vb - Imports Microsoft.VisualStudio.TestTools.UnitTesting - - Namespace StringLibraryTest - - Public Class UnitTest1 - - Sub TestSub() - - End Sub - End Class - End Namespace - ``` - - The source code created by the unit test template does the following: - - - It imports the namespace, which contains the types used for unit testing. - - It applies the attribute to the `UnitTest1` class. - - It applies the attribute to define `TestMethod1` in C# or `TestSub` in Visual Basic. - - Each method tagged with [[TestMethod]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute) in a test class tagged with [[TestClass]](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute) is executed automatically when the unit test is run. - -## Add a project reference - -For the test project to work with the `StringLibrary` class, add a reference in the **StringLibraryTest** project to the `StringLibrary` project. - -1. In **Solution Explorer**, right-click the **Dependencies** node of the **StringLibraryTest** project and select **Add Project Reference** from the context menu. - -1. In the **Reference Manager** dialog, expand the **Projects** node, and select the box next to **StringLibrary**. Adding a reference to the `StringLibrary` assembly allows the compiler to find **StringLibrary** methods while compiling the **StringLibraryTest** project. - -1. Select **OK**. - -## Add and run unit test methods - -When Visual Studio runs a unit test, it executes each method that is marked with the attribute in a class that is marked with the attribute. A test method ends when the first failure is found or when all tests contained in the method have succeeded. - -The most common tests call members of the class. Many assert methods include at least two parameters, one of which is the expected test result and the other of which is the actual test result. Some of the `Assert` class's most frequently called methods are shown in the following table: - -| Assert methods | Function | -| ------------------ | -------- | -| `Assert.AreEqual` | Verifies that two values or objects are equal. The assert fails if the values or objects aren't equal. | -| `Assert.AreSame` | Verifies that two object variables refer to the same object. The assert fails if the variables refer to different objects. | -| `Assert.IsFalse` | Verifies that a condition is `false`. The assert fails if the condition is `true`. | -| `Assert.IsNotNull` | Verifies that an object isn't `null`. The assert fails if the object is `null`. | - -You can also use the method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown. - -In testing the `StringLibrary.StartsWithUpper` method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return `true` in these cases, so you can call the method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return `false` in these cases, so you can call the method. - -Since your library method handles strings, you also want to make sure that it successfully handles an [empty string (`String.Empty`)](xref:System.String.Empty), a valid string that has no characters and whose is 0, and a `null` string that hasn't been initialized. You can call `StartsWithUpper` directly as a static method and pass a single argument. Or you can call `StartsWithUpper` as an extension method on a `string` variable assigned to `null`. - -You'll define three methods, each of which calls an method for each element in a string array. You'll call a method overload that lets you specify an error message to be displayed in case of test failure. The message identifies the string that caused the failure. - -To create the test methods: - -1. In the *UnitTest1.cs* or *UnitTest1.vb* code window, replace the code with the following code: - - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibraryTest/UnitTest1.cs"::: - :::code language="vb" source="./snippets/library-with-visual-studio/vb/StringLibraryTest/UnitTest1.vb"::: - - The test of uppercase characters in the `TestStartsWithUpper` method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the `TestDoesNotStartWithUpper` method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). - -1. On the menu bar, select **File** > **Save UnitTest1.cs As** or **File** > **Save UnitTest1.vb As**. In the **Save File As** dialog, select the arrow beside the **Save** button, and select **Save with Encoding**. - - :::image type="content" source="./media/testing-library-with-visual-studio/save-file-as-dialog.png" alt-text="Visual Studio Save File As dialog"::: - -1. In the **Confirm Save As** dialog, select the **Yes** button to save the file. - -1. In the **Advanced Save Options** dialog, select **Unicode (UTF-8 with signature) - Codepage 65001** from the **Encoding** drop-down list and select **OK**. - - :::image type="content" source="./media/testing-library-with-visual-studio/advanced-save-options.png" alt-text="Visual Studio Advanced Save Options dialog"::: - - If you fail to save your source code as a UTF8-encoded file, Visual Studio may save it as an ASCII file. When that happens, the runtime doesn't accurately decode the UTF8 characters outside of the ASCII range, and the test results won't be correct. - -1. On the menu bar, select **Test** > **Run All Tests**. If the **Test Explorer** window doesn't open, open it by choosing **Test** > **Test Explorer**. The three tests are listed in the **Passed Tests** section, and the **Summary** section reports the result of the test run. - - :::image type="content" source="./media/testing-library-with-visual-studio/test-explorer-window.png" alt-text="Test Explorer window with passing tests"::: - -## Handle test failures - -If you're doing test-driven development (TDD), you write tests first and they fail the first time you run them. Then you add code to the app that makes the test succeed. For this tutorial, you created the test after writing the app code that it validates, so you haven't seen the test fail. To validate that a test fails when you expect it to fail, add an invalid value to the test input. - -1. Modify the `words` array in the `TestDoesNotStartWithUpper` method to include the string "Error". You don't need to save the file because Visual Studio automatically saves open files when a solution is built to run tests. - - ```csharp - string[] words = { "alphabet", "Error", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " }; - ``` - - ```vb - Dim words() As String = { "alphabet", "Error", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " } - - ``` - -1. Run the test by selecting **Test** > **Run All Tests** from the menu bar. The **Test Explorer** window indicates that two tests succeeded and one failed. - - :::image type="content" source="./media/testing-library-with-visual-studio/failed-test-window.png" alt-text="Test Explorer window with failing tests"::: - -1. Select the failed test, `TestDoesNotStartWith`. - - The **Test Explorer** window displays the message produced by the assert: "Assert.IsFalse failed. Expected for 'Error': false; actual: True". Because of the failure, no strings in the array after "Error" were tested. - - :::image type="content" source="./media/testing-library-with-visual-studio/failed-test-detail.png" alt-text="Test Explorer window showing the IsFalse assertion failure"::: - -1. Remove the string "Error" that you added in step 1. Rerun the test and the tests pass. - -## Test the Release version of the library - -Now that the tests have all passed when running the Debug build of the library, run the tests an additional time against the Release build of the library. A number of factors, including compiler optimizations, can sometimes produce different behavior between Debug and Release builds. - -To test the Release build: - -1. In the Visual Studio toolbar, change the build configuration from **Debug** to **Release**. - - :::image type="content" source="./media/testing-library-with-visual-studio/visual-studio-toolbar-release.png" alt-text="Visual Studio toolbar with release build highlighted"::: - -1. In **Solution Explorer**, right-click the **StringLibrary** project and select **Build** from the context menu to recompile the library. - - :::image type="content" source="./media/testing-library-with-visual-studio/build-library-context-menu.png" alt-text="StringLibrary context menu with build command"::: - -1. Run the unit tests by choosing **Test** > **Run All Tests** from the menu bar. The tests pass. - -## Debug tests - -If you're using Visual Studio as your IDE, you can use the same process shown in [Tutorial: Debug a .NET console application using Visual Studio](debugging-with-visual-studio.md) to debug code using your unit test project. Instead of starting the *ShowCase* app project, right-click the **StringLibraryTests** project, and select **Debug Tests** from the context menu. - -Visual Studio starts the test project with the debugger attached. Execution will stop at any breakpoint you've added to the test project or the underlying library code. - -## Additional resources - -- [Unit test basics - Visual Studio](/visualstudio/test/unit-test-basics) -- [Unit testing in .NET](../testing/index.md) - -## Next steps - -In this tutorial, you unit tested a class library. You can make the library available to others by publishing it to [NuGet](https://nuget.org) as a package. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Create and publish a NuGet package using Visual Studio](/nuget/quickstart/create-and-publish-a-package-using-visual-studio?tabs=netcore-cli) - -If you publish a library as a NuGet package, others can install and use it. To learn how, follow a NuGet tutorial: - -> [!div class="nextstepaction"] -> [Install and use a package in Visual Studio](/nuget/quickstart/install-and-use-a-package-in-visual-studio) - -A library doesn't have to be distributed as a package. It can be bundled with a console app that uses it. To learn how to publish a console app, see the earlier tutorial in this series: - -> [!div class="nextstepaction"] -> [Publish a .NET console application using Visual Studio](publishing-with-visual-studio.md) - -::: zone-end diff --git a/docs/core/tutorials/with-visual-studio-code.md b/docs/core/tutorials/with-visual-studio-code.md index ffe7a95b42b65..eea79d99d5b4b 100644 --- a/docs/core/tutorials/with-visual-studio-code.md +++ b/docs/core/tutorials/with-visual-studio-code.md @@ -2,7 +2,6 @@ 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: 09/12/2024 -zone_pivot_groups: dotnet-version --- # Tutorial: Create a .NET console application using Visual Studio Code @@ -103,244 +102,3 @@ In this tutorial, you created a .NET console application. In the next tutorial, > [Debug a .NET console application using Visual Studio Code](debugging-with-visual-studio-code.md) ::: zone-end - -::: zone pivot="dotnet-7-0" - -This tutorial shows how to create and run a .NET console application by using Visual Studio Code and the .NET CLI. Project tasks, such as creating, compiling, and running a project are done by using the .NET CLI. You can follow this tutorial with a different code editor and run commands in a terminal if you prefer. - -## Prerequisites - -* [Visual Studio Code](https://code.visualstudio.com/) with the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) installed. For information about how to install extensions on Visual Studio Code, see [VS Code Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-gallery). -* The [.NET 7 SDK](https://dotnet.microsoft.com/download/dotnet/7.0). - -## Create the app - -Create a .NET console app project named "HelloWorld". - -1. Start Visual Studio Code. - -1. Select **File** > **Open Folder** (**File** > **Open...** on macOS) from the main menu. - -1. In the **Open Folder** dialog, create a *HelloWorld* folder and select it. Then click **Select Folder** (**Open** on macOS). - - The folder name becomes the project name and the namespace name by default. You'll add code later in the tutorial that assumes the project namespace is `HelloWorld`. - -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. Open the **Terminal** in Visual Studio Code by selecting **View** > **Terminal** from the main menu. - - The **Terminal** opens with the command prompt in the *HelloWorld* folder. - -1. In the **Terminal**, enter the following command: - - ```dotnetcli - dotnet new console --framework net7.0 - ``` - - The project template creates a simple application that displays "Hello, World" in the console window by calling the method in *Program.cs*. - - ```csharp - Console.WriteLine("Hello, World!"); - ``` - -1. Replace the contents of *Program.cs* with the following code: - - ```csharp - namespace HelloWorld - { - class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } - } - } - ``` - - The first time you edit a *.cs* file, Visual Studio Code prompts you to add the missing assets to build and debug your app. Select **Yes**, and Visual Studio Code creates a *.vscode* folder with *launch.json* and *tasks.json* files. - - > [!NOTE] - > If you don't get the prompt, or if you accidentally dismiss it without selecting **Yes**, do the following steps to create *launch.json* and *tasks.json*: - > - >* Select **Run** > **Add Configuration** from the menu. - >* Select **.NET 5+ and .NET Core** at the **Select environment** prompt. - - 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. - - In the latest version of C#, a new feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) lets you omit the `Program` class and the `Main` method. Most existing C# programs don't use top-level statements, so this tutorial doesn't use this new feature. But it's available in C# 10, and whether you use it in your programs is a matter of style preference. - -## Run the app - -Run the following command in the **Terminal**: - -```dotnetcli -dotnet run -``` - -The program displays "Hello, World!" and ends. - -![The dotnet run command](media/with-visual-studio-code/dotnet-run-command.png) - -## Enhance the app - -Enhance the application to prompt the user for their name and display it along with the date and time. - -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: - - :::code language="csharp" source="./snippets/with-visual-studio/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 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. 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](../../csharp/language-reference/tokens/interpolated.md). - -1. 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. - -1. Run the program again: - - ```dotnetcli - dotnet run - ``` - -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"::: - -1. Press any key to exit the program. - -## Additional resources - -* [Setting up Visual Studio Code](https://code.visualstudio.com/docs/setup/setup-overview) - -## 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 using Visual Studio Code](debugging-with-visual-studio-code.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -This tutorial shows how to create and run a .NET console application by using Visual Studio Code and the .NET CLI. Project tasks, such as creating, compiling, and running a project are done by using the .NET CLI. You can follow this tutorial with a different code editor and run commands in a terminal if you prefer. - -## Prerequisites - -* [Visual Studio Code](https://code.visualstudio.com/) with the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) installed. For information about how to install extensions on Visual Studio Code, see [VS Code Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-gallery). -* The [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0). - -## Create the app - -Create a .NET console app project named "HelloWorld". - -1. Start Visual Studio Code. - -1. Select **File** > **Open Folder** (**File** > **Open...** on macOS) from the main menu. - -1. In the **Open Folder** dialog, create a *HelloWorld* folder and select it. Then click **Select Folder** (**Open** on macOS). - - The folder name becomes the project name and the namespace name by default. You'll add code later in the tutorial that assumes the project namespace is `HelloWorld`. - -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. Open the **Terminal** in Visual Studio Code by selecting **View** > **Terminal** from the main menu. - - The **Terminal** opens with the command prompt in the *HelloWorld* folder. - -1. In the **Terminal**, enter the following command: - - ```dotnetcli - dotnet new console --framework net6.0 --use-program-main - ``` - - The project template creates a simple application that displays "Hello, World" in the console window by calling the method in *Program.cs*. - - ```csharp - namespace HelloWorld; - - class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } - } - ``` - - The first time you edit a *.cs* file, Visual Studio Code prompts you to add the missing assets to build and debug your app. Select **Yes**, and Visual Studio Code creates a *.vscode* folder with *launch.json* and *tasks.json* files. - - > [!NOTE] - > If you don't get the prompt, or if you accidentally dismiss it without selecting **Yes**, do the following steps to create *launch.json* and *tasks.json*: - > - >* Select **Run** > **Add Configuration** from the menu. - >* Select **.NET 5+ and .NET Core** at the **Select environment** prompt. - - 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. - - In the latest version of C#, a new feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) lets you omit the `Program` class and the `Main` method. Most existing C# programs don't use top-level statements, so this tutorial doesn't use this new feature. But it's available in C# 10, and whether you use it in your programs is a matter of style preference. In the `dotnet new` command that you used to create the project, the `--use-program-main` option prevented top-level statements from being used. - -## Run the app - -Run the following command in the **Terminal**: - -```dotnetcli -dotnet run -``` - -The program displays "Hello, World!" and ends. - -![The dotnet run command](media/with-visual-studio-code/dotnet-run-command.png) - -## Enhance the app - -Enhance the application to prompt the user for their name and display it along with the date and time. - -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: - - :::code language="csharp" source="./snippets/with-visual-studio/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 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. 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](../../csharp/language-reference/tokens/interpolated.md). - -1. 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. - -1. Run the program again: - - ```dotnetcli - dotnet run - ``` - -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"::: - -1. Press any key to exit the program. - -## Additional resources - -* [Setting up Visual Studio Code](https://code.visualstudio.com/docs/setup/setup-overview) - -## 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 using Visual Studio Code](debugging-with-visual-studio-code.md) - -::: zone-end diff --git a/docs/core/tutorials/with-visual-studio.md b/docs/core/tutorials/with-visual-studio.md index 0b3f692e29a07..c3033c7d7f12a 100644 --- a/docs/core/tutorials/with-visual-studio.md +++ b/docs/core/tutorials/with-visual-studio.md @@ -2,7 +2,6 @@ title: Create a .NET console application using Visual Studio description: Learn how to create a .NET console application with C# or Visual Basic using Visual Studio. ms.date: 03/21/2024 -zone_pivot_groups: dotnet-version dev_langs: - "csharp" - "vb" @@ -10,8 +9,6 @@ ms.custom: vs-dotnet --- # Tutorial: Create a .NET console application using Visual Studio -::: zone pivot="dotnet-9-0,dotnet-8-0" - This tutorial shows how to create and run a .NET console application in Visual Studio 2022. ## Prerequisites @@ -119,237 +116,3 @@ In this tutorial, you created a .NET console application. In the next tutorial, > [!div class="nextstepaction"] > [Debug a .NET console application using Visual Studio](debugging-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-7-0" - -This tutorial shows how to create and run a .NET console application in Visual Studio 2022. - -## Prerequisites - -- [Visual Studio 2022 version 17.4 or later](https://visualstudio.microsoft.com/downloads/?utm_medium=microsoft&utm_source=learn.microsoft.com&utm_campaign=inline+link&utm_content=download+vs2022) with the **.NET desktop development** workload installed. The .NET 7 SDK is automatically installed when you select this workload. - - For more information, see [Install the .NET SDK with Visual Studio](../install/windows.md#install-with-visual-studio). - -## Create the app - -Create a .NET console app project named "HelloWorld". - -1. Start Visual Studio 2022. - -1. On the start page, choose **Create a new project**. - - :::image type="content" source="./media/with-visual-studio/start-window-2022.png" alt-text="Create a new project button selected on the Visual Studio start page"::: - -1. 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/with-visual-studio/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. - -1. In the **Configure your new project** dialog, enter **HelloWorld** in the **Project name** box. Then choose **Next**. - - :::image type="content" source="./media/with-visual-studio/configure-new-project.png" alt-text="Configure your new project window with Project name, location, and solution name fields"::: - -1. In the **Additional information** dialog: - - Select **.NET 7 (Standard-term support)**. - - Select **Do not use top-level statements**. - - Select **Create**. - - 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: - - ```csharp - namespace HelloWorld; - - internal class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } - } - ``` - - ```vb - 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 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. - - In the latest version of C#, a new feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) lets you omit the `Program` class and the `Main` method. Most existing C# programs don't use top-level statements, so this tutorial doesn't use this new feature. But it's available in C# 10, and whether you use it in your programs is a matter of style preference. - -## 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. (Or "Hello World!" without a comma in the Visual Basic project template.) - - :::image type="content" source="./media/with-visual-studio/hello-world-console-net6.png" alt-text="Console window showing Hello World Press any key to continue"::: - -1. 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: - - :::code language="csharp" source="./snippets/with-visual-studio-6-0/csharp/Program.cs" id="MainMethod"::: - :::code language="vb" source="./snippets/with-visual-studio/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 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. 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](../../csharp/language-reference/tokens/interpolated.md). - -1. Press Ctrl+F5 to run the program without debugging. - -1. Respond to the prompt by entering a name and pressing the Enter key. - - :::image type="content" source="./media/with-visual-studio/hello-world-update-net6.png" alt-text="Console window with modified program output"::: - -1. Press any key to close the console window. - -## Additional resources - -- [Standard-term support (STS) releases and long-term support (LTS) releases](../releases-and-support.md) - -## 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 using Visual Studio](debugging-with-visual-studio.md) - -::: zone-end - -::: zone pivot="dotnet-6-0" - -This tutorial shows how to create and run a .NET console application in Visual Studio 2022. - -## Prerequisites - -- [Visual Studio 2022 version 17.1 or later](https://visualstudio.microsoft.com/downloads/?utm_medium=microsoft&utm_source=learn.microsoft.com&utm_campaign=inline+link&utm_content=download+vs2022) with the **.NET desktop development** workload installed. The .NET 6 SDK is automatically installed when you select this workload. - - For more information, see [Install the .NET SDK with Visual Studio](../install/windows.md#install-with-visual-studio). - -## Create the app - -Create a .NET console app project named "HelloWorld". - -1. Start Visual Studio 2022. - -1. On the start page, choose **Create a new project**. - - :::image type="content" source="./media/with-visual-studio/start-window-2022.png" alt-text="Create a new project button selected on the Visual Studio start page"::: - -1. 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**. - - :::image type="content" source="./media/with-visual-studio/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. - -1. In the **Configure your new project** dialog, enter **HelloWorld** in the **Project name** box. Then choose **Next**. - - :::image type="content" source="./media/with-visual-studio/configure-new-project.png" alt-text="Configure your new project window with Project name, location, and solution name fields"::: - -1. In the **Additional information** dialog, select **.NET 6 (Long-term support)**, and then select **Create**. - - 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: - - ```csharp - Console.WriteLine("Hello, World!"); - ``` - - ```vb - 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. - -1. For C#, the code is just a line that calls the method to display "Hello World!" in the console window. Replace the contents of *Program.cs* with the following code: - - ```csharp - namespace HelloWorld - { - class Program - { - static void Main(string[] args) - { - Console.WriteLine("Hello World!"); - } - } - } - ``` - - ```vb - ' This step of the tutorial applies only to C#. - ``` - - 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. - - In the latest version of C#, a new feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) lets you omit the `Program` class and the `Main` method. Most existing C# programs don't use top-level statements, so this tutorial doesn't use this new feature. But it's available in C# 10, and whether you use it in your programs is a matter of style preference. - -## 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. - - :::image type="content" source="./media/with-visual-studio/hello-world-console-net6.png" alt-text="Console window showing Hello World Press any key to continue"::: - -1. 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: - - :::code language="csharp" source="./snippets/with-visual-studio-6-0/csharp/Program.cs" id="MainMethod"::: - :::code language="vb" source="./snippets/with-visual-studio/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 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. 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](../../csharp/language-reference/tokens/interpolated.md). - -1. Press Ctrl+F5 to run the program without debugging. - -1. Respond to the prompt by entering a name and pressing the Enter key. - - :::image type="content" source="./media/with-visual-studio/hello-world-update-net6.png" alt-text="Console window with modified program output"::: - -1. Press any key to close the console window. - -## Additional resources - -- [Releases and support for .NET](../releases-and-support.md) - -## 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 using Visual Studio](debugging-with-visual-studio.md) - -::: zone-end diff --git a/docs/standard/serialization/system-text-json/migrate-from-newtonsoft.md b/docs/standard/serialization/system-text-json/migrate-from-newtonsoft.md index 730acd1e57f09..cc6b8d3469145 100644 --- a/docs/standard/serialization/system-text-json/migrate-from-newtonsoft.md +++ b/docs/standard/serialization/system-text-json/migrate-from-newtonsoft.md @@ -587,7 +587,7 @@ Starting in .NET 9, you can customize the indentation character and size for Date: Fri, 22 Nov 2024 14:54:11 -0800 Subject: [PATCH 2/4] removed duplicated code snippets --- .../library-with-visual-studio-code.md | 4 +- .../tutorials/library-with-visual-studio.md | 4 +- .../csharp/ShowCase/Program.cs | 37 ------------- .../csharp/ShowCase/ShowCase.csproj | 14 ----- .../csharp/StringLibrary/Class1.cs | 14 ----- .../csharp/StringLibrary/StringLibrary.csproj | 9 ---- .../StringLibraryTest.csproj | 21 -------- .../csharp/StringLibraryTest/UnitTest1.cs | 52 ------------------- .../csharp/HelloWorld.csproj | 9 ---- .../with-visual-studio-6-0/csharp/Program.cs | 16 ------ ...testing-library-with-visual-studio-code.md | 2 +- .../core/tutorials/with-visual-studio-code.md | 4 -- docs/core/tutorials/with-visual-studio.md | 2 +- 13 files changed, 6 insertions(+), 182 deletions(-) delete mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs delete mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj delete mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs delete mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj delete mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj delete mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs delete mode 100644 docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj delete mode 100644 docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs diff --git a/docs/core/tutorials/library-with-visual-studio-code.md b/docs/core/tutorials/library-with-visual-studio-code.md index aa9c4089bae8a..5d6bf035c6b4b 100644 --- a/docs/core/tutorials/library-with-visual-studio-code.md +++ b/docs/core/tutorials/library-with-visual-studio-code.md @@ -55,7 +55,7 @@ Start by creating a .NET class library project named "StringLibrary" and an asso 1. Open *Class1.cs* and replace the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibrary/Class1.cs"::: The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. @@ -93,7 +93,7 @@ Add a console application that uses the class library. The app will prompt the u 1. Open *ShowCase/Program.cs* and replace all of the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/ShowCase/Program.cs"::: The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. diff --git a/docs/core/tutorials/library-with-visual-studio.md b/docs/core/tutorials/library-with-visual-studio.md index 7a7c258871d05..a97c6c6554a53 100644 --- a/docs/core/tutorials/library-with-visual-studio.md +++ b/docs/core/tutorials/library-with-visual-studio.md @@ -59,7 +59,7 @@ To create the blank solution: 1. Replace the code in the code window for *Class1.cs* or *Class1.vb* with the following code, and save the file. If the language you want to use isn't shown, change the language selector at the top of the page. - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibrary/Class1.cs"::: :::code language="vb" source="./snippets/library-with-visual-studio/vb/StringLibrary/Class1.vb"::: The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. @@ -86,7 +86,7 @@ Add a console application that uses the class library. The app will prompt the u 1. In the code window for the *Program.cs* or *Program.vb* file, replace all of the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/ShowCase/Program.cs"::: :::code language="vb" source="./snippets/library-with-visual-studio/vb/ShowCase/Program.vb"::: The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs deleted file mode 100644 index aabb5011a29d3..0000000000000 --- a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs +++ /dev/null @@ -1,37 +0,0 @@ -using UtilityLibraries; - -class Program -{ - static void Main(string[] args) - { - int row = 0; - - do - { - if (row == 0 || row >= 25) - ResetConsole(); - - string? input = Console.ReadLine(); - if (string.IsNullOrEmpty(input)) break; - Console.WriteLine($"Input: {input}"); - Console.WriteLine("Begins with uppercase? " + - $"{(input.StartsWithUpper() ? "Yes" : "No")}"); - Console.WriteLine(); - row += 4; - } while (true); - return; - - // Declare a ResetConsole local method - void ResetConsole() - { - if (row > 0) - { - Console.WriteLine("Press any key to continue..."); - Console.ReadKey(); - } - Console.Clear(); - Console.WriteLine($"{Environment.NewLine}Press only to exit; otherwise, enter a string and press :{Environment.NewLine}"); - row = 3; - } - } -} diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj deleted file mode 100644 index 4c98f8720c509..0000000000000 --- a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - Exe - net6 - enable - enable - - - - - - - diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs deleted file mode 100644 index 6c49b2e8093c9..0000000000000 --- a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace UtilityLibraries; - -public static class StringLibrary -{ - public static bool StartsWithUpper(this string? str) - { - if (string.IsNullOrWhiteSpace(str)) - return false; - - char ch = str[0]; - return char.IsUpper(ch); - } -} - diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj deleted file mode 100644 index 0da2a6f35b586..0000000000000 --- a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net6.0 - enable - enable - - - diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj deleted file mode 100644 index a1b44b21c4edb..0000000000000 --- a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - net6 - enable - enable - false - - - - - - - - - - - - - - diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs deleted file mode 100644 index d9248ca96ad20..0000000000000 --- a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using UtilityLibraries; - -namespace StringLibraryTest; - -[TestClass] -public class UnitTest1 -{ - [TestMethod] - public void TestStartsWithUpper() - { - // Tests that we expect to return true. - string[] words = { "Alphabet", "Zebra", "ABC", "Αθήνα", "Москва" }; - foreach (var word in words) - { - bool result = word.StartsWithUpper(); - Assert.IsTrue(result, - string.Format("Expected for '{0}': true; Actual: {1}", - word, result)); - } - } - - [TestMethod] - public void TestDoesNotStartWithUpper() - { - // Tests that we expect to return false. - string[] words = { "alphabet", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", - "1234", ".", ";", " " }; - foreach (var word in words) - { - bool result = word.StartsWithUpper(); - Assert.IsFalse(result, - string.Format("Expected for '{0}': false; Actual: {1}", - word, result)); - } - } - - [TestMethod] - public void DirectCallWithNullOrEmpty() - { - // Tests that we expect to return false. - string?[] words = { string.Empty, null }; - foreach (var word in words) - { - bool result = StringLibrary.StartsWithUpper(word); - Assert.IsFalse(result, - string.Format("Expected for '{0}': false; Actual: {1}", - word == null ? "" : word, result)); - } - } -} - diff --git a/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj b/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj deleted file mode 100644 index 9a77342a90c73..0000000000000 --- a/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - Exe - net6.0 - enable - enable - - diff --git a/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs b/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs deleted file mode 100644 index ac9f87f87c99d..0000000000000 --- a/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace HelloWorld; - -public class HelloWorld -{ - public static void Main(string[] args) - { - // - 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 any key to exit..."); - Console.ReadKey(true); - // - } -} diff --git a/docs/core/tutorials/testing-library-with-visual-studio-code.md b/docs/core/tutorials/testing-library-with-visual-studio-code.md index 155049cb65641..dc5f017791c2d 100644 --- a/docs/core/tutorials/testing-library-with-visual-studio-code.md +++ b/docs/core/tutorials/testing-library-with-visual-studio-code.md @@ -79,7 +79,7 @@ To create the test methods: 1. Open *StringLibraryTest/UnitTest1.cs* and replace all of the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibraryTest/UnitTest1.cs"::: The test of uppercase characters in the `TestStartsWithUpper` method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the `TestDoesNotStartWithUpper` method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). diff --git a/docs/core/tutorials/with-visual-studio-code.md b/docs/core/tutorials/with-visual-studio-code.md index eea79d99d5b4b..24d094e592132 100644 --- a/docs/core/tutorials/with-visual-studio-code.md +++ b/docs/core/tutorials/with-visual-studio-code.md @@ -5,8 +5,6 @@ ms.date: 09/12/2024 --- # Tutorial: Create a .NET console application using Visual Studio Code -::: zone pivot="dotnet-9-0,dotnet-8-0" - This tutorial shows how to create and run a .NET console application by using Visual Studio Code. ## Prerequisites @@ -100,5 +98,3 @@ In this tutorial, you created a .NET console application. In the next tutorial, > [!div class="nextstepaction"] > [Debug a .NET console application using Visual Studio Code](debugging-with-visual-studio-code.md) - -::: zone-end diff --git a/docs/core/tutorials/with-visual-studio.md b/docs/core/tutorials/with-visual-studio.md index c3033c7d7f12a..f365eecbaaded 100644 --- a/docs/core/tutorials/with-visual-studio.md +++ b/docs/core/tutorials/with-visual-studio.md @@ -89,7 +89,7 @@ Enhance the application to prompt the user for their name and display it along w 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: - :::code language="csharp" source="./snippets/with-visual-studio-6-0/csharp/Program.cs" id="MainMethod"::: + :::code language="csharp" source="./snippets/with-visual-studio/csharp/Program.cs" id="MainMethod"::: :::code language="vb" source="./snippets/with-visual-studio/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 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. From 81c49b55f873a864a1804c2e83209d9f55d68e3f Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:55:23 -0800 Subject: [PATCH 3/4] Revert "removed duplicated code snippets" This reverts commit c3fd4bddcfd23b0fa9b01a45cf14affadcef7997. --- .../library-with-visual-studio-code.md | 4 +- .../tutorials/library-with-visual-studio.md | 4 +- .../csharp/ShowCase/Program.cs | 37 +++++++++++++ .../csharp/ShowCase/ShowCase.csproj | 14 +++++ .../csharp/StringLibrary/Class1.cs | 14 +++++ .../csharp/StringLibrary/StringLibrary.csproj | 9 ++++ .../StringLibraryTest.csproj | 21 ++++++++ .../csharp/StringLibraryTest/UnitTest1.cs | 52 +++++++++++++++++++ .../csharp/HelloWorld.csproj | 9 ++++ .../with-visual-studio-6-0/csharp/Program.cs | 16 ++++++ ...testing-library-with-visual-studio-code.md | 2 +- .../core/tutorials/with-visual-studio-code.md | 4 ++ docs/core/tutorials/with-visual-studio.md | 2 +- 13 files changed, 182 insertions(+), 6 deletions(-) create mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs create mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj create mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs create mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj create mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj create mode 100644 docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs create mode 100644 docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj create mode 100644 docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs diff --git a/docs/core/tutorials/library-with-visual-studio-code.md b/docs/core/tutorials/library-with-visual-studio-code.md index 5d6bf035c6b4b..aa9c4089bae8a 100644 --- a/docs/core/tutorials/library-with-visual-studio-code.md +++ b/docs/core/tutorials/library-with-visual-studio-code.md @@ -55,7 +55,7 @@ Start by creating a .NET class library project named "StringLibrary" and an asso 1. Open *Class1.cs* and replace the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibrary/Class1.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. @@ -93,7 +93,7 @@ Add a console application that uses the class library. The app will prompt the u 1. Open *ShowCase/Program.cs* and replace all of the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/ShowCase/Program.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. diff --git a/docs/core/tutorials/library-with-visual-studio.md b/docs/core/tutorials/library-with-visual-studio.md index a97c6c6554a53..7a7c258871d05 100644 --- a/docs/core/tutorials/library-with-visual-studio.md +++ b/docs/core/tutorials/library-with-visual-studio.md @@ -59,7 +59,7 @@ To create the blank solution: 1. Replace the code in the code window for *Class1.cs* or *Class1.vb* with the following code, and save the file. If the language you want to use isn't shown, change the language selector at the top of the page. - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibrary/Class1.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs"::: :::code language="vb" source="./snippets/library-with-visual-studio/vb/StringLibrary/Class1.vb"::: The class library, `UtilityLibraries.StringLibrary`, contains a method named `StartsWithUpper`. This method returns a value that indicates whether the current string instance begins with an uppercase character. The Unicode standard distinguishes uppercase characters from lowercase characters. The method returns `true` if a character is uppercase. @@ -86,7 +86,7 @@ Add a console application that uses the class library. The app will prompt the u 1. In the code window for the *Program.cs* or *Program.vb* file, replace all of the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/ShowCase/Program.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs"::: :::code language="vb" source="./snippets/library-with-visual-studio/vb/ShowCase/Program.vb"::: The code uses the `row` variable to maintain a count of the number of rows of data written to the console window. Whenever it's greater than or equal to 25, the code clears the console window and displays a message to the user. diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs new file mode 100644 index 0000000000000..aabb5011a29d3 --- /dev/null +++ b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/Program.cs @@ -0,0 +1,37 @@ +using UtilityLibraries; + +class Program +{ + static void Main(string[] args) + { + int row = 0; + + do + { + if (row == 0 || row >= 25) + ResetConsole(); + + string? input = Console.ReadLine(); + if (string.IsNullOrEmpty(input)) break; + Console.WriteLine($"Input: {input}"); + Console.WriteLine("Begins with uppercase? " + + $"{(input.StartsWithUpper() ? "Yes" : "No")}"); + Console.WriteLine(); + row += 4; + } while (true); + return; + + // Declare a ResetConsole local method + void ResetConsole() + { + if (row > 0) + { + Console.WriteLine("Press any key to continue..."); + Console.ReadKey(); + } + Console.Clear(); + Console.WriteLine($"{Environment.NewLine}Press only to exit; otherwise, enter a string and press :{Environment.NewLine}"); + row = 3; + } + } +} diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj new file mode 100644 index 0000000000000..4c98f8720c509 --- /dev/null +++ b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/ShowCase/ShowCase.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6 + enable + enable + + + + + + + diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs new file mode 100644 index 0000000000000..6c49b2e8093c9 --- /dev/null +++ b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/Class1.cs @@ -0,0 +1,14 @@ +namespace UtilityLibraries; + +public static class StringLibrary +{ + public static bool StartsWithUpper(this string? str) + { + if (string.IsNullOrWhiteSpace(str)) + return false; + + char ch = str[0]; + return char.IsUpper(ch); + } +} + diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj new file mode 100644 index 0000000000000..0da2a6f35b586 --- /dev/null +++ b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibrary/StringLibrary.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj new file mode 100644 index 0000000000000..a1b44b21c4edb --- /dev/null +++ b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/StringLibraryTest.csproj @@ -0,0 +1,21 @@ + + + + net6 + enable + enable + false + + + + + + + + + + + + + + diff --git a/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs new file mode 100644 index 0000000000000..d9248ca96ad20 --- /dev/null +++ b/docs/core/tutorials/snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs @@ -0,0 +1,52 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using UtilityLibraries; + +namespace StringLibraryTest; + +[TestClass] +public class UnitTest1 +{ + [TestMethod] + public void TestStartsWithUpper() + { + // Tests that we expect to return true. + string[] words = { "Alphabet", "Zebra", "ABC", "Αθήνα", "Москва" }; + foreach (var word in words) + { + bool result = word.StartsWithUpper(); + Assert.IsTrue(result, + string.Format("Expected for '{0}': true; Actual: {1}", + word, result)); + } + } + + [TestMethod] + public void TestDoesNotStartWithUpper() + { + // Tests that we expect to return false. + string[] words = { "alphabet", "zebra", "abc", "αυτοκινητοβιομηχανία", "государство", + "1234", ".", ";", " " }; + foreach (var word in words) + { + bool result = word.StartsWithUpper(); + Assert.IsFalse(result, + string.Format("Expected for '{0}': false; Actual: {1}", + word, result)); + } + } + + [TestMethod] + public void DirectCallWithNullOrEmpty() + { + // Tests that we expect to return false. + string?[] words = { string.Empty, null }; + foreach (var word in words) + { + bool result = StringLibrary.StartsWithUpper(word); + Assert.IsFalse(result, + string.Format("Expected for '{0}': false; Actual: {1}", + word == null ? "" : word, result)); + } + } +} + diff --git a/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj b/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj new file mode 100644 index 0000000000000..9a77342a90c73 --- /dev/null +++ b/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/HelloWorld.csproj @@ -0,0 +1,9 @@ + + + + Exe + net6.0 + enable + enable + + diff --git a/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs b/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs new file mode 100644 index 0000000000000..ac9f87f87c99d --- /dev/null +++ b/docs/core/tutorials/snippets/with-visual-studio-6-0/csharp/Program.cs @@ -0,0 +1,16 @@ +namespace HelloWorld; + +public class HelloWorld +{ + public static void Main(string[] args) + { + // + 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 any key to exit..."); + Console.ReadKey(true); + // + } +} diff --git a/docs/core/tutorials/testing-library-with-visual-studio-code.md b/docs/core/tutorials/testing-library-with-visual-studio-code.md index dc5f017791c2d..155049cb65641 100644 --- a/docs/core/tutorials/testing-library-with-visual-studio-code.md +++ b/docs/core/tutorials/testing-library-with-visual-studio-code.md @@ -79,7 +79,7 @@ To create the test methods: 1. Open *StringLibraryTest/UnitTest1.cs* and replace all of the code with the following code. - :::code language="csharp" source="./snippets/library-with-visual-studio/csharp/StringLibraryTest/UnitTest1.cs"::: + :::code language="csharp" source="./snippets/library-with-visual-studio-6-0/csharp/StringLibraryTest/UnitTest1.cs"::: The test of uppercase characters in the `TestStartsWithUpper` method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the `TestDoesNotStartWithUpper` method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433). diff --git a/docs/core/tutorials/with-visual-studio-code.md b/docs/core/tutorials/with-visual-studio-code.md index 24d094e592132..eea79d99d5b4b 100644 --- a/docs/core/tutorials/with-visual-studio-code.md +++ b/docs/core/tutorials/with-visual-studio-code.md @@ -5,6 +5,8 @@ ms.date: 09/12/2024 --- # Tutorial: Create a .NET console application using Visual Studio Code +::: zone pivot="dotnet-9-0,dotnet-8-0" + This tutorial shows how to create and run a .NET console application by using Visual Studio Code. ## Prerequisites @@ -98,3 +100,5 @@ In this tutorial, you created a .NET console application. In the next tutorial, > [!div class="nextstepaction"] > [Debug a .NET console application using Visual Studio Code](debugging-with-visual-studio-code.md) + +::: zone-end diff --git a/docs/core/tutorials/with-visual-studio.md b/docs/core/tutorials/with-visual-studio.md index f365eecbaaded..c3033c7d7f12a 100644 --- a/docs/core/tutorials/with-visual-studio.md +++ b/docs/core/tutorials/with-visual-studio.md @@ -89,7 +89,7 @@ Enhance the application to prompt the user for their name and display it along w 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: - :::code language="csharp" source="./snippets/with-visual-studio/csharp/Program.cs" id="MainMethod"::: + :::code language="csharp" source="./snippets/with-visual-studio-6-0/csharp/Program.cs" id="MainMethod"::: :::code language="vb" source="./snippets/with-visual-studio/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 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. From 7e342889d1104809cf75ec048d718142cf6de5bb Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:06:38 -0800 Subject: [PATCH 4/4] remove zone pivots --- docs/core/tutorials/with-visual-studio-code.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/core/tutorials/with-visual-studio-code.md b/docs/core/tutorials/with-visual-studio-code.md index eea79d99d5b4b..24d094e592132 100644 --- a/docs/core/tutorials/with-visual-studio-code.md +++ b/docs/core/tutorials/with-visual-studio-code.md @@ -5,8 +5,6 @@ ms.date: 09/12/2024 --- # Tutorial: Create a .NET console application using Visual Studio Code -::: zone pivot="dotnet-9-0,dotnet-8-0" - This tutorial shows how to create and run a .NET console application by using Visual Studio Code. ## Prerequisites @@ -100,5 +98,3 @@ In this tutorial, you created a .NET console application. In the next tutorial, > [!div class="nextstepaction"] > [Debug a .NET console application using Visual Studio Code](debugging-with-visual-studio-code.md) - -::: zone-end