C# 简介

语言定位、CLR/JIT/GC、以及 .NET 各版本的关系。

Beginner
Key Points.NET 是平台,C# 是语言,CLR 是运行时,三者不是一回事。

三个概念

TermWhat it is
C#语言,语法和类型系统
.NET平台,含运行时、基础库、SDK 工具
CLR.NET 的运行时,负责 JIT、GC、类型加载
IL中间语言,编译器产物

从源码到运行

Text
Program.cs
   │  csc / dotnet build
   ▼
Program.dll  (IL + 元数据)
   │  dotnet run / CLR 加载
   ▼
JIT 编译成机器码  →  执行

JIT can optimise for the actual CPU (AVX2, runtime inlining) at the cost of warm-up. .NET 7+ also supports AOT, producing native executables with faster startup.

GC

C#
// 托管对象不用手动释放
var list = new List<int> { 1, 2, 3 };
// 出了作用域,GC 会在合适时机回收

// 非托管资源用 using 确保释放
using var file = File.OpenText("a.txt");
Console.WriteLine(file.ReadLine());

C#'s GC is generational (gen 0/1/2): new objects start in gen 0 and survivors get promoted. Most die in gen 0, so collection is cheap. using handles deterministic release of file handles and connections.

版本演进

VersionYearNotable
C# 1.02002基础面向对象
C# 2.02005泛型、可空类型
C# 3.02007LINQ、var、lambda
C# 5.02012async/await
C# 8.02019可空引用类型、模式匹配增强
C# 9.02020record、顶级语句
C# 122023主构造函数、集合表达式
.NET Framework (4.8 and earlier) is Windows-only; .NET 5+ is cross-platform. New projects should target .NET 8 or later, never .NET Framework.

Friends & Partners