C# in Depth 第四版总复习:15章整书验收
以同一真实系统横穿第四版15章,沿type、execution、data shape和memory四条contract chain完成历史语义、现代边界与发布证据验收。
学习目标
- 能分析public SDK、data pipeline、interop host或hot parser,逐Part写出15章相关的真实风险与证据
- 能绘制type、execution、shape和memory四条端到端contract chain,判断最早可拒绝错误的boundary
- 能设计identity、content、learning、engineering与promotion五类整书门禁,复现17个活动页面和整书100分证据
为什么总复习必须让15章作用于同一个系统
能解释generic variance不代表会设计跨assembly SDK;知道LINQ deferred execution不代表会管理DbContext lifetime;会写async method不代表理解generated state、context和cleanup;会用Span不代表能证明view没有逃逸。整书验收把这些feature放在同一boundary,检查连接处是否仍保持type、execution、shape和lifetime contract。
先预测:为15章各写一个isolated snippet是否足够;compiler warning为零是否证明runtime null-safe;使用tuple names是否等于public schema稳定;benchmark显示一次更快是否能证明ref API值得发布。四个答案都是否。
↡选择同一真实系统boundary,让第四版15章共同解释它的type、execution、shape、failure和lifetime。第一轮:按四个Part扫描系统
Part 1: Context and version model
先记录language version、compiler SDK、target runtime/BCL与consumer matrix。一个feature“不工作”必须定位到parser/type checker、lowering、runtime capability还是library surface。Modern note和book-time preview分栏,避免用今天结果重写2019语境。
Part 2: C# 2-5 foundations
检查generic constraints和nullable values是否准确表达capability/absence;delegate、iterator、lambda和LINQ是否让capture与deferred execution逃出owner scope;dynamic/COM boundary是否被typed adapter限制;async API是否区分Task completion、context、cancellation与state-machine cleanup。
Part 3: C# 6 concision
检查property/initializer是否保留construction invariant,expression bodies是否隐藏side effects;interpolation/FormattableString的culture owner和structured data是否明确;nameof是否只用于source-coupled names;null conditional与filters是否表达合法absence和handler ownership,而非吞掉fault。
Part 4: C# 7 and beyond
检查tuple positions/names是否越过public boundary,Deconstruct和patterns是否形成total ordered classifier;in/readonly/ref return/ref-like views是否有alias/lifetime proof与measurement;concise calls是否产生source compatibility依赖;nullable references、ranges与async streams是否覆盖runtime boundary、allocation和cleanup。
切换SDK、pipeline、interop与parser
boundary: Repository.StreamSummaries
type: IAsyncEnumerable<(int id, decimal total)>
execution: provider query -> async enumeration
lifetime: request DbContext + enumerator cleanup
failure: translation/cancel/consumer fault preserve owner policy第二轮:沿四条Contract Chain追踪
Type chain
External input先进入typed adapter,经generic constraints/inference/variance,到LINQ expression或dynamic boundary,再进入tuple/pattern和nullable reference contract。每个stage写static type、runtime representation和invalid state policy。能在compile/boundary拒绝的错误不应拖到binder、provider或NullReferenceException。
Execution chain
从delegate invocation、iterator MoveNext和query construction,追到async state machine suspension、exception filter search、async stream MoveNextAsync与DisposeAsync。记录实际次数、thread/context、evaluation order、short-circuit、cancellation observation和failure propagation。
Data-shape chain
从constructor/initializer/property和string formatting,追到tuple arity/positions、Deconstruct outputs、pattern arms和external schema。Internal names可随refactor,external names/version不可静默变化;culture、null和unknown subtype都要有owner。
Memory and lifetime chain
从value copy、boxing、closure/state capture,追到in/readonly defensive copy、ref alias、Span slice和ref-like escape。所有optimization先建立semantic equivalence与lifetime proof,再以真实JIT/runtime measurement决定是否保留。
↡从external input到generic、provider、tuple/pattern和nullable boundary持续记录static与runtime type的链路。 ↡从callback/iterator到async stream,记录evaluation、suspension、filter、cancellation和cleanup的链路。切换type、execution、shape与memory chain
await using var session = await factory.OpenAsync(token);
var rows = await session.Orders.Where(specification).ToListAsync(token);
var summary = (count: rows.Count, total: rows.Sum(row => row.Total));
return summary.total >= threshold ? Result.Accept(summary) : Result.Reject(summary);第三轮:Compatibility与Version Matrix
至少建立五轴matrix:book-time LangVersion、modern LangVersion、compiler SDK、target runtime/BCL、consumer boundary。C# 8 syntax可能由compiler支持,但相关runtime/library type未必在目标framework可用;public tuple names与named arguments还会影响source recompilation;dynamic/COM依赖runtime binder与external metadata。
Historical fidelity使用两列记录:Original claim只解释该章当时语言版本;Modern update标first later version、变化和仍不变的invariant。例如Chapter 11原始内容是tuples,C# 9 records只在alternatives列;Chapter 15先保留preview uncertainty,再列shipping behavior。
↡同时覆盖LangVersion、compiler、runtime/BCL、source consumer和binary/external consumer的兼容性测试表。original chapter | book-time behavior | modern addition | invariant | compile/runtime/consumer evidence第四轮:整书发布门禁
- Identity:Jon Skeet、Manning、Fourth Edition、March 2019、ISBN 9781617294532一致。
- Outline:4 Parts/15 Chapters与first-level concepts覆盖100%,无invented chapter,Chapter 11不混入records。
- Content:17个active pages;15章、导学、总复习全部chapter score 100,3个专属互动视觉与practice齐全。
- Wiring:navigation顺序为0导学、1-15官方章、16总复习;review questions与labels使用活动slugs;旧topic refs为0。
- Engineering:MDX、type、targeted lint、diff checks全绿;全库225本未完成前不push/deploy。
切换identity、content、learning、engineering与promotion
本章回顾:15章最终收敛成四条可执行链
- Four-Part scan保证context、foundations、concision和shape/lifetime无遗漏。
- Type chain把invalid state前移;execution chain让deferred、async与failure时序可观察。
- Shape chain保护position/name/culture/schema;memory chain保护copy、alias与referent lifetime。
- Version matrix区分original、preview、shipping和modern additions,保持历史忠实与当前可用性。
- 整书只有identity、outline、content、wiring和engineering evidence全部100后才完成。
练习
问题 1:Public SDK返回named tuple并启用nullable,升级时如何联合审计?
问题 2:Async iterator capture DbContext,consumer early break后仍占连接,怎样定位?
问题 3:本书完成的最终证据是什么?
术语表
名词解释
本章出现的专业名词,用大白话再讲一遍。
- whole-book system audit
- end-to-end type chain
- end-to-end execution chain
- version compatibility matrix
- release evidence chain