Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating tutorials for C# Dev KIt #41440

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 23 additions & 73 deletions docs/core/tutorials/debugging-with-visual-studio-code.md

Large diffs are not rendered by default.

356 changes: 58 additions & 298 deletions docs/core/tutorials/library-with-visual-studio-code.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/core/tutorials/publishing-with-visual-studio-code.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
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: 08/30/2023
ms.date: 09/12/2024
zone_pivot_groups: dotnet-version
---
# Tutorial: Publish a .NET console application using Visual Studio Code
Expand Down Expand Up @@ -91,13 +91,13 @@ In the following steps, you'll look at the files created by the publish process.

1. On Linux, enter `./HelloWorld` and press <kbd>Enter</kbd>.

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

1. On any platform, run the app by using the [`dotnet`](../tools/dotnet.md) command:

1. Enter `dotnet HelloWorld.dll` and press <kbd>Enter</kbd>.

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

## Additional resources

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//Variant of Program.cs that reads input from the debug console instead of the terminal
#if READ
// <MainMethod>
Console.WriteLine("What is your name?");
var name = Console.ReadLine();
var currentDate = DateTime.Now;
Console.WriteLine($"{Environment.NewLine}Hello, {name}, on {currentDate:d} at {currentDate:t}!");
Console.Write($"{Environment.NewLine}Press Enter to exit...");
Console.Read();
// </MainMethod>
#endif
175 changes: 40 additions & 135 deletions docs/core/tutorials/testing-library-with-visual-studio-code.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 19 additions & 43 deletions docs/core/tutorials/with-visual-studio-code.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
---
title: Create a .NET console application using Visual Studio Code
description: Learn how to create a .NET console application using Visual Studio Code and the .NET CLI.
ms.date: 09/21/2023
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

::: zone pivot="dotnet-8-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.
This tutorial shows how to create and run a .NET console application by using Visual Studio Code.

## Prerequisites

* [Visual Studio Code](https://code.visualstudio.com/) with the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) installed.

If you have the [C# Dev Kit extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit) installed, uninstall or disable it. It isn't used by this tutorial series.
* [Visual Studio Code](https://code.visualstudio.com/) with [C# Dev Kit](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).

Expand All @@ -26,25 +24,19 @@ 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. 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. In the **Open Folder** dialog, create a *HelloWorld* folder and select it. Then click **Select Folder** (**Open** on macOS).
1. After selecting the command, you need to choose the project template. Choose **Console App**.

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. Select the location where you would like the new project to be created.

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.
1. Give your new project a name, "HelloWorld".

The **Terminal** opens with the command prompt in the *HelloWorld* folder.
1. Select to **Show all template options**. Set **Do not use top-level statements** to **true**. And finally, select **Create Project**.

1. In the **Terminal**, enter the following command:

```dotnetcli
dotnet new console --framework net8.0 --use-program-main
```
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.

Open the *Program.cs* file to see the simple application created by the template:
1. Open the *Program.cs* file to see the simple application created by the template:

```csharp
namespace HelloWorld;
Expand All @@ -58,30 +50,18 @@ Create a .NET console app project named "HelloWorld".
}
```

The first time you open a *.cs* file, Visual Studio Code prompts you to add 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 <xref:System.String> array as an argument. `Main` is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the *args* array. The code in `Main` calls the <xref:System.Console.WriteLine(System.String)?displayProperty=nameWithType> method to display a message in the console window.

C# has a feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) that lets you omit the `Program` class and the `Main` method. This tutorial doesn't use this feature. Whether you use it in your programs is a matter of style preference. In the `dotnet new` command that created the project, the `--use-program-main` option prevented top-level statements from being used.
C# has a feature named [top-level statements](../../csharp/fundamentals/program-structure/top-level-statements.md) that lets you omit the `Program` class and the `Main` method. This tutorial doesn't use this feature. Whether you use it in your programs is a matter of style preference. By setting **Do not use top-level statements to true** when you created the project, you prevented top-level statements from being used.

## Run the app

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

```dotnetcli
dotnet run
```
If asked to select a debugger, select **C#**, then select **C#: HelloWorld**

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.
Expand All @@ -90,7 +70,7 @@ Enhance the application to prompt the user for their name and display it along w

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":::
:::code language="csharp" source="./snippets/with-visual-studio/csharp/Program-Read.cs" id="MainMethod":::

This code displays a prompt in the console window and waits until the user enters a string followed by the <kbd>Enter</kbd> key. It stores this string in a variable named `name`. It also retrieves the value of the <xref:System.DateTime.Now?displayProperty=nameWithType> property, which contains the current local time, and assigns it to a variable named `currentDate`. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the <xref:System.Console.ReadKey(System.Boolean)?displayProperty=nameWithType> method to wait for user input.

Expand All @@ -103,17 +83,13 @@ Enhance the application to prompt the user for their name and display it along w
> [!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. Select **Run**>**Run without debugging**.

1. Respond to the prompt by entering a name and pressing the <kbd>Enter</kbd> 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.
1. Press <kbd>Enter</kbd> to exit the program.

## Additional resources

Expand Down Expand Up @@ -182,7 +158,7 @@ Create a .NET console app project named "HelloWorld".
}
```

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.
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*:
Expand Down Expand Up @@ -299,7 +275,7 @@ Create a .NET console app project named "HelloWorld".
}
```

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.
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*:
Expand Down
Loading