学习地图:C# 10 in a Nutshell 官方 25 章
按 Joseph Albahari《C# 10 in a Nutshell》官方25章建立语言、.NET数据、运行时服务、元数据与低层边界学习路径。
学习目标
- 能描述官方25章的六段依赖路径,并定位当前问题的前置章节
- 能分析type、owner、boundary与evidence四类阅读问题
- 能设计predict、trace、break、transfer学习循环,并用产物证明而非阅读进度验收
为什么学习地图必须服从原书目录
本书不是十个“热门特性”的选读集,而是从C#语言规则一路推进到.NET数据、资源与I/O、metadata/dynamic、并发低层和text engine的25章系统参考。跳过中段会产生假理解:会写async却不懂Dispose和Stream owner,会用reflection却不懂assembly identity,会用Span却忽略native lifetime。
先预测:只学第1-4章能设计可靠网络服务吗;掌握Task就等于掌握thread safety吗;会写P/Invoke签名就不必学GC/pinning吗;Regex validation不需要资源上限吗;读完25章就自动具备迁移能力吗。答案都是否。
↡以官方25章为唯一骨架,所有页面、复习题和验收证据都能映射回对应章节。路径一:Language(Chapters 1-4)
- Introducing C# and .NET:对象、类型安全、内存、平台、CLR/BCL/runtime与C# 10变化。
- C# Language Basics:编译、语法、类型、数值、字符串、数组、变量、表达式、语句与namespace。
- Creating Types in C#:class/inheritance/object/struct/interface/enum/nested type/generic。
- Advanced C#:delegate/event/lambda/exception/iterator/nullability/record/pattern/attribute/dynamic/operator/unsafe。
这四章建立编译器与类型系统模型。验收不是背语法,而是能预测overload、conversion、allocation、dispatch、exception和nullable flow。
public abstract record Outcome;
public sealed record Accepted(Guid Id) : Outcome;
public sealed record Rejected(string Code) : Outcome;
static string Describe(Outcome outcome) => outcome switch
{
Accepted { Id: var id } => $"accepted:{id:N}",
Rejected { Code: var code } => $"rejected:{code}",
_ => throw new UnreachableException()
};这段代码同时要求type hierarchy、record value model、property pattern与exhaustiveness reasoning。
路径二:.NET and Data(Chapters 5-11)
- .NET Overview:runtime、BCL、system types、兼容与application layers。
- .NET Fundamentals:text、date/time、format/parse、globalization、number、Guid、equality/order。
- Collections:enumeration、list/array/set/dictionary、immutable collection与comparer。
- LINQ Queries:query expression、deferred execution、composition、provider expression tree与EF Core。
- LINQ Operators:filter/project/join/order/group/set/aggregate/quantifier等operator semantics。
- LINQ to XML:X-DOM、functional construction、query/update、namespace与streaming projection。
- Other XML and JSON Technologies:XmlReader/Writer与System.Text.Json streaming/DOM lifetime。
这一段的共同门禁是semantic identity:字符串比较用什么culture,DateTime表示什么时间线,Dictionary/GroupBy如何判等,LINQ在内存还是provider执行,JSON missing/null/unknown如何映射。
切换language、.NET/data、runtime与advanced路线
路径三:Runtime Services(Chapters 12-17)
- Disposal and Garbage Collection:IDisposable、roots、generations、finalizers、pool、leak与weak reference。
- Diagnostics:Debug/Trace、process/thread/stack、counter、Stopwatch与跨平台toolchain。
- Concurrency and Asynchrony:thread/pool/context/Task/async stream/cancel/combinator/async lock。
- Streams and I/O:partial I/O、adapter、compression、atomic file、OS security与memory map。
- Networking:address/URI、HttpClient/server、DNS、SMTP与TCP framing。
- Assemblies:manifest/metadata/IL、signing/resources、load context与plugin lifecycle。
Runtime路线的核心是owner timeline:谁创建、谁等待、谁取消、谁释放、失败时半成品是否可见。GC只管理不可达managed memory,不替你关闭stream、停止task或卸载plugin。
static async Task<Result> FetchAsync(HttpClient client, Uri uri, CancellationToken ct)
{
using HttpResponseMessage response = await client.SendAsync(
new HttpRequestMessage(HttpMethod.Get, uri),
HttpCompletionOption.ResponseHeadersRead, ct);
response.EnsureSuccessStatusCode();
await using Stream body = await response.Content.ReadAsStreamAsync(ct);
return await ParseBoundedAsync(body, maxBytes: 1_000_000, ct);
}一段看似普通的HTTP代码同时穿过URI identity、handler/response/stream ownership、async cancellation、partial I/O、JSON limits和diagnostics。
路径四:Metadata and Dynamic(Chapters 18-20)
- Reflection and Metadata:Type/member binding、attribute、dynamic code、emit与IL parse。
- Dynamic Programming:DLR、runtime overload、visitor、DynamicObject/Expando与language bridge。
- Cryptography:DPAPI、hash/HMAC、password KDF、AEAD、key management与public-key signing。
三章都在处理“晚绑定边界”:reflection延迟成员选择,dynamic延迟类型检查,cryptographic envelope延迟按key/version解释bytes。晚绑定必须用identity、allowlist、version和fail-closed补回静态保障。
切换type、owner、boundary与evidence
路径五:Concurrency and Low-level(Chapters 21-24)
- Advanced Threading:exclusive/nonexclusive locks、signals、Barrier、Lazy、thread local与timer。
- Parallel Programming:PFX、PLINQ、Parallel、Task scheduler、aggregate fault、concurrent queue。
- Span<T> and Memory<T>:slice/copy/text、async memory、enumerator、stack/native storage。
- Native and COM Interoperability:P/Invoke、marshaling、callback、shared memory与COM双向边界。
这一路线风险最高,因为错误可能跨thread、heap、process和ABI延迟爆发。所有优化先有sequential/safe baseline,再证明throughput、allocation或interop需求值得增加复杂度。
static async Task ConsumeAsync(ChannelReader<IMemoryOwner<byte>> reader, CancellationToken ct)
{
await foreach (IMemoryOwner<byte> owner in reader.ReadAllAsync(ct))
{
using (owner)
Process(owner.Memory.Span);
}
}这里必须同时守住bounded queue、async completion、pooled memory owner与Span同步lifetime。
路径六:Text(Chapter 25)
- Regular Expressions:engine/compile/options、escape/set/quantifier、greedy/lazy、assertion/lookaround/anchor/group、replace/split与recipes。
Regex是受预算约束的小程序。Pattern正确只是起点,还要限制input/output、显式timeout,并用near-miss证明不会因回溯阻塞服务。
↡每章用预测、状态/owner追踪、故障注入和陌生案例迁移形成的四步验收。每章学习与验收节奏
切换predict、trace、break与transfer
本章回顾:从目录走向工程判断
- 25章是连续体系,不是十个主题采样。
- Language定义合法程序,Data定义语义,Runtime定义owner和completion。
- Metadata/dynamic需要晚绑定安全壳,low-level需要精确lifetime。
- 每章必须有预测、追踪、负测和迁移证据。
- 最终目标是能为新问题选择正确chapter、contract和gate。
练习
问题 1:设计HTTP插件服务的最小学习路径。
问题 2:怎样证明不是“读完”而是“掌握”第14章?
问题 3:为什么第25章仍依赖前面章节?
术语表
名词解释
本章出现的专业名词,用大白话再讲一遍。
- official-outline spine
- semantic reading lens
- late-binding safety envelope
- predict-trace-break-transfer loop
- chapter evidence artifact