C# Overview & .NET Environment Setup
What C# and .NET are, cross-platform .NET 8, the dotnet CLI, project structure, and your first web endpoint.
C# and Modern .NET
C# is a strongly typed, multi-paradigm language running on .NET: garbage collected, memory-safe by default, with async built into the language. Since .NET Core, it is fully cross-platform — the same code runs on Linux servers, Windows, macOS and containers, which is why it is now a mainstream backend choice.
- Best for: enterprise APIs, microservices, cloud-native workloads, gRPC and high-throughput servers.
- LTS cadence: .NET 8 is the current LTS, free and open source, with AOT compilation available.
dotnet CLI and Project Layout
# Windows/macOS/Linux 官网下载 .NET SDK,或包管理器:
# winget install Microsoft.DotNet.SDK.8 / brew install dotnet / apt install dotnet-sdk-8.0
dotnet --info # 查看 SDK 与运行时
dotnet new webapi -n Shop # 生成最小 API 项目
cd Shop
dotnet restore # 还原 NuGet 包
dotnet build
dotnet run # 监听 http://localhost:5000
dotnet publish -c Release -o out # 发布到 out/
# 自包含发布(目标机无需装运行时)
dotnet publish -c Release --self-contained -r linux-x64
Shop/
├── Shop.csproj # 项目文件(SDK 风格,依赖声明在这)
├── Program.cs # 入口(C# 10 起可顶级语句)
├── appsettings.json # 配置
├── Properties/launchSettings.json
└── Shop.sln # 解决方案(多项目)
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.*" />
</ItemGroup>
</Project>
C# in Ten Minutes
// 顶级语句(Program.cs 全文就是这个)
var name = args.Length > 0 ? args[0] : "BackendWiki";
Console.WriteLine($"Hello, {name}!");
// 可空引用类型:string? 可能为 null
string? text = MaybeNull();
var len = text?.Length ?? 0; // 安全导航 + 默认值
// LINQ 一行筛选
int[] xs = { 5, 1, 9, 3 };
var big = xs.Where(x => x > 2).OrderBy(x => x).ToArray(); // [3,5,9]
// async/await
await Task.Delay(100);
Console.WriteLine("done");
string? MaybeNull() => null;
Your First Endpoint
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
app.MapGet("/health", () => new { ok = true });
app.MapGet("/hello", (string? name) => $"Hello, {name ?? "BackendWiki"}");
app.Run(); // 阻塞直至关闭C# 与现代 .NET
C# 是运行在 .NET 上的强类型多范式语言:带 GC、默认内存安全、语言级 async。自 .NET Core 起完全跨平台——同一份代码跑在 Linux 服务器、Windows、macOS 与容器上,因此它如今已是主流后端选择。
- 最适合:企业级 API、微服务、云原生负载、gRPC 与高吞吐服务。
- 发布节奏:.NET 8 是当前 LTS,免费开源,支持 AOT 编译。
dotnet CLI 与项目结构
# Windows/macOS/Linux 官网下载 .NET SDK,或包管理器:
# winget install Microsoft.DotNet.SDK.8 / brew install dotnet / apt install dotnet-sdk-8.0
dotnet --info # 查看 SDK 与运行时
dotnet new webapi -n Shop # 生成最小 API 项目
cd Shop
dotnet restore # 还原 NuGet 包
dotnet build
dotnet run # 监听 http://localhost:5000
dotnet publish -c Release -o out # 发布到 out/
# 自包含发布(目标机无需装运行时)
dotnet publish -c Release --self-contained -r linux-x64
Shop/
├── Shop.csproj # 项目文件(SDK 风格,依赖声明在这)
├── Program.cs # 入口(C# 10 起可顶级语句)
├── appsettings.json # 配置
├── Properties/launchSettings.json
└── Shop.sln # 解决方案(多项目)
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.*" />
</ItemGroup>
</Project>
十分钟 C# 速览
// 顶级语句(Program.cs 全文就是这个)
var name = args.Length > 0 ? args[0] : "BackendWiki";
Console.WriteLine($"Hello, {name}!");
// 可空引用类型:string? 可能为 null
string? text = MaybeNull();
var len = text?.Length ?? 0; // 安全导航 + 默认值
// LINQ 一行筛选
int[] xs = { 5, 1, 9, 3 };
var big = xs.Where(x => x > 2).OrderBy(x => x).ToArray(); // [3,5,9]
// async/await
await Task.Delay(100);
Console.WriteLine("done");
string? MaybeNull() => null;
第一个接口
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
app.MapGet("/health", () => new { ok = true });
app.MapGet("/hello", (string? name) => $"Hello, {name ?? "BackendWiki"}");
app.Run(); // 阻塞直至关闭