C# Overview & .NET Environment Setup

What C# and .NET are, cross-platform .NET 8, the dotnet CLI, project structure, and your first web endpoint.

BeginnerUpdated 2026-10-3044K views

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();                   // 阻塞直至关闭

Friends & Partners