.NET Tutorial - Hello World in 5 minutes
Intro
Purpose
Install .NET, Visual Studio Code, and create your first application.
Prerequisites
500 MB of available hard-drive space.
- 500 MB of available hard-drive space.
- macOS 12.0 or later versions.
Time to complete
5 minutes + download/installation time
Scenario
A simple application written in C# that prints Hello, World!
to your VS Code console.
Not ready to install anything?
You can try our in-browser tutorial instead.
Download and install
若要生成第一个 .NET 应用程序,需要安装 .NET SDK、Visual Studio Code 和 C# 开发工具包。
.NET SDK (软件开发工具包)是使用 C# 生成和运行应用程序所需的工具和库的免费开源集合。C# 是你将在本教程中使用的面向对象的新式编程语言。
Visual Studio Code (VS Code)是全球最受欢迎的轻型、免费的开源代码编辑器。你将在此处编写 C# 代码。
C# 开发工具包是一组功能强大的 VS Code 扩展,可使 C# 开发更轻松。它可帮助你在生成应用时编写、测试和调试代码。
.NET Winget 配置文件将为你安装 .NET 8 SDK、VS Code 和 C# 开发工具包。如果已安装某些内容,Winget 将跳过该安装步骤。
To build your first .NET application with Visual Studio Code, set up your environment:
-
Download and install the .NET SDK:
Download .NET 8 SDK x64 (Intel)
Download .NET 8 SDK Arm64 (Apple Silicon)If you're on a Mac with an Apple M1 or M2 chip, you need to install the Arm64 version of the SDK.
-
Download and install VS Code:
-
Open VS Code and select the Extensions button in VS Code's Activity Bar to the left. Type C# in the search bar, select C# Dev Kit, and then select the Install button on the C# Dev Kit extension page.
单击下面的链接可下载文件。
打开下载文件夹,然后双击该文件以开始安装。
选择终端或开发人员主页安装说明
双击该文件后,将打开一个终端,你将看到 Winget 配置文件已准备好安装 .NET 8 SDK、VS Code (如果没有)和 C# 开发工具包。
阅读许可协议并同意,方法是在终端中键入“y”,然后按 Enter。
注意: 你有责任了解要选择执行的配置设置。Microsoft 不对你创作或导入的配置文件负责。此配置可能会更改 Windows 中的设置、安装软件、更改软件设置(包括安全设置),并代表你接受第三方程序包和服务的用户协议。运行此配置文件即表示你确认理解并同意这些资源和设置。任何安装的应用程序均由其所有者授权给你。NuGet 既不对第三方程序包或服务负责,也不授予其许可证。
Winget 现在正在安装入门所需的一切内容!完成大约需要 5 分钟,完成后,会在终端中看到下面的输出。
双击该文件后,将打开一个开发人员主页,你将看到 Winget 配置文件已准备好安装 .NET 8 SDK、VS Code (如果没有)和 C# 开发工具包。
阅读许可协议并同意,方法是单击左下角的“我同意并想要继续”,然后单击右下角的“设置为管理员”。在系统询问是否要允许开发人员主页对设备进行更改时,选择“是”。
注意: 你有责任了解要选择执行的配置设置。Microsoft 不对你创作或导入的配置文件负责。此配置可能会更改 Windows 中的设置、安装软件、更改软件设置(包括安全设置),并代表你接受第三方程序包和服务的用户协议。运行此配置文件即表示你确认理解并同意这些资源和设置。任何安装的应用程序均由其所有者授权给你。NuGet 既不对第三方程序包或服务负责,也不授予其许可证。
开发人员主页现在正在安装入门所需的一切内容!完成大约需要 5 分钟,完成后,会在窗口中看到下面的输出。
Check everything installed correctly
现在已设置环境,请转到 Windows 任务栏中的搜索,然后键入 Visual Studio Code。单击应用程序以打开 VS Code。
Once you've set up your environment, open a new VS Code terminal. In the toolbar, select Terminal, then New Terminal.
注意: 请注意,如果通过终端打开 VS Code,则需要启用工作区信任。
现在,通过转到工具栏并选择终端,然后选择新终端来打开新终端。
In your terminal, run the following command to check your installation.
dotnet
If the installation succeeded, you should see an output similar to the following:
Usage: dotnet [options]
Usage: dotnet [path-to-application]
Options:
-h|--help Display help.
--info Display .NET information.
--list-sdks Display the installed SDKs.
--list-runtimes Display the installed runtimes.
path-to-application:
The path to an application .dll file to execute.
If everything looks good, select the Continue button below to go to the next step.
Got an error?
If you receive a 'dotnet' is not recognized as an internal or external command error, make sure you opened a new VS Code terminal. If restarting VS Code or restarting your machine doesn't resolve the issue, use the I ran into an issue button to get help fixing the problem.
Create your app
- 按 CTRL+SHIFT+P,在 VS Code 中打开命令面板。
- 按 CMD+SHIFT+P,在 VS Code 中打开命令面板。
- Type .NET: to see the commands you can run with C# Dev Kit!
- Find and select .NET: New Project to create a new .NET project.
- Scroll down and select Console App.
- Choose the folder location you'd like to save your project.
- 出现提示时,在命令面板中将项目命名为
MyConsoleApp
。
选中复选框并选择是,我信任作者以启用工作区信任。
If you have a Visual Studio Subscription, sign into your account. If you do not see a prompt pop up, click on the C# icon in VS Code's Status Bar towards the bottom right of your window.
在边栏中,确保 VS Code 资源管理器已打开。此处将显示 MyConsoleApp
文件夹和解决方案资源管理器。按照以下视频进行操作: 折叠 MyConsoleApp
文件夹,打开 解决方案资源管理器,打开 MyConsoleApp
项目,然后将解决方案资源管理器拖放到更高的位置。
- 折叠 VS Code 资源管理器中的
MyConsoleApp
文件夹。 - 单击解决方案资源管理器左侧的 V 形图标可将其展开。
- 单击解决方案资源管理器中
MyConsoleApp
项目的左侧 V 形图标可将其展开。 - 将解决方案资源管理器拖放到 MyConsoleApp 下。
The main file in the MyConsoleApp
folder is called Program.cs
. By default, it already contains the necessary code to write Hello, World!
to the terminal. Click on the file to see the code created by the template:
Select the Continue button below to go to the next step.
Got an error?
If you can't resolve the issue you're having, select the I ran into an issue button below to get help fixing the problem.
Run your app
To run your application, select the dropdown menu to the right of the run button in the top right, and select Run project associated with this file.
You should see Hello, World!
in your VS Code terminal.
Hello, World!
Congratulations, you've built and run your first .NET app! Select the Continue button below to go to the next step.
Edit your code
In the Program.cs
file, add the highlighted line after the code that prints Hello, World!
, like the following:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Console.WriteLine("The current time is " + DateTime.Now);
Save the Program.cs
file and select Run project associated with the file again. If you succeed, you should see an output similar to the following:
Hello, World!
The current time is 2024/10/14 上午10:46:59
Next steps
Congratulations, you've built and run your first .NET app!
Keep learning
To keep learning general .NET skills, try our tutorials on Microsoft Learn where you'll learn about .NET, dependencies, working with files, debugging, and more:
Build .NET applications learning path
Learn C#
C# is .NET's modern, innovative, open-source programming language for building all your apps. Get started by trying our C# interactive tutorials on Microsoft Learn:
Take your first steps with C# on Microsoft Learn
.NET for Beginners
Let Claudia walk you through the basics of .NET with her beginner video series:
You might also be interested in...