C# 开发环境

装 .NET SDK、用 dotnet CLI、配 VS / VS Code。

Beginner
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.0
Bash
dotnet --version
dotnet --list-sdks
Output
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 run
Output
Hello, World!
TemplateCreates
console控制台应用
webASP.NET Core 空项目
webapiWeb 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/

编辑器

ToolPlatformNote
Visual Studio 2022Windows功能最全,调试和设计器强
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.

Friends & Partners