C# 开发环境
装 .NET SDK、用 dotnet CLI、配 VS / VS Code。
Key Points装 **SDK** 而不是 Runtime —— SDK 包含编译器和 CLI,Runtime 只能跑不能编。
安装
Bash
# Windows
winget install Microsoft.DotNet.SDK.8
# macOS
brew install --cask dotnet-sdk
# Linux(Ubuntu)
sudo apt install dotnet-sdk-8.0Bash
dotnet --version
dotnet --list-sdksOutput
10.0.401
8.0.404 [C:\Program Files\dotnet\sdk]
10.0.401 [C:\Program Files\dotnet\sdk]创建项目
Bash
dotnet new console -o Hello
cd Hello
dotnet runOutput
Hello, World!| Template | Creates |
|---|---|
console | 控制台应用 |
web | ASP.NET Core 空项目 |
webapi | Web API |
classlib | 类库 |
xunit | 测试项目 |
maui | 跨平台桌面/移动 |
常用命令
Bash
dotnet new console -o App # 新建
dotnet build # 编译
dotnet run # 编译并运行
dotnet test # 跑测试
dotnet publish -c Release # 发布
dotnet add package Newtonsoft.Json
dotnet list package
dotnet watch run # 改代码自动重启Output
$ dotnet publish -c Release -r linux-x64 --self-contained
App -> bin/Release/net8.0/linux-x64/publish/编辑器
| Tool | Platform | Note |
|---|---|---|
| Visual Studio 2022 | Windows | 功能最全,调试和设计器强 |
| VS Code + C# Dev Kit | 全平台 | 轻量,免费 |
| Rider | 全平台 | JetBrains 出品,付费 |
项目文件
XML
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>ImplicitUsings auto-imports System, System.Linq and friends; Nullable enables nullable reference type checking. Both are on by default for new projects — keep them.
dotnet run compiles for you, so just run it again after edits. During development use dotnet watch run to restart on save.安装
Bash
# Windows
winget install Microsoft.DotNet.SDK.8
# macOS
brew install --cask dotnet-sdk
# Linux(Ubuntu)
sudo apt install dotnet-sdk-8.0Bash
dotnet --version
dotnet --list-sdksOutput
10.0.401
8.0.404 [C:\Program Files\dotnet\sdk]
10.0.401 [C:\Program Files\dotnet\sdk]创建项目
Bash
dotnet new console -o Hello
cd Hello
dotnet runOutput
Hello, World!| 模板 | 创建 |
|---|---|
console | 控制台应用 |
web | ASP.NET Core 空项目 |
webapi | Web API |
classlib | 类库 |
xunit | 测试项目 |
maui | 跨平台桌面/移动 |
常用命令
Bash
dotnet new console -o App # 新建
dotnet build # 编译
dotnet run # 编译并运行
dotnet test # 跑测试
dotnet publish -c Release # 发布
dotnet add package Newtonsoft.Json
dotnet list package
dotnet watch run # 改代码自动重启Output
$ dotnet publish -c Release -r linux-x64 --self-contained
App -> bin/Release/net8.0/linux-x64/publish/编辑器
| 工具 | 平台 | 说明 |
|---|---|---|
| Visual Studio 2022 | Windows | 功能最全,调试和设计器强 |
| VS Code + C# Dev Kit | 全平台 | 轻量,免费 |
| Rider | 全平台 | JetBrains 出品,付费 |
项目文件
XML
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>ImplicitUsings 自动导入 System、System.Linq 等常用命名空间;Nullable 开启可空引用类型检查。这两项新项目默认开启,建议保持。
dotnet run 会自动编译,改完代码直接再跑一次即可,不必手动 build。开发时用 dotnet watch run,保存文件就自动重启。