导学:Essential C# 7.0 官方22章学习地图
按第6版官方22章建立五阶段学习路径、先修依赖、阶段交付物与失败回补路线。
学习目标
- 能解释官方22章从language rules、type design、abstraction、concurrency/interop到CLI runtime的先修关系
- 能设计符合目标岗位的顺序路线,同时保留不能跳过的chapter dependency和阶段验收
- 能分析“读过但不会用”的失败证据,并定位到应回补的章节、实验与可观察产物
为什么学习地图必须忠于官方22章
旧的十章压缩路线把classes、interfaces、value types、exceptions、collections、reflection、threads、interop和CLI等独立主题合并或遗漏,会造成两个问题:读者无法从左侧导航知道原书真实覆盖范围,遇到event leak、deferred query、deadlock或ABI corruption时也找不到定义该规则的章节。
本地图以Essential C# 7.0第6版的22章为唯一chapter spine。它不替代原章,也不把标题重新发明成泛化主题;作用是说明“为什么先读这一章、读完要交付什么、失败后回到哪里”。先预测:如果只会写LINQ语法却说不清delegate、enumerator和deferred execution,应回补哪几章;如果P/Invoke只在x86正常,应从哪些type/runtime章节查起。
↡与权威目录一一对应、用于决定导航顺序、覆盖范围和复习定位的完整章节序列。五阶段总览:从Language到Runtime
五阶段不是把22章重新压成五章,而是为连续阅读设置checkpoints。每一章仍有独立正文、图示、练习与复习题。
- Language mechanics(第1-5章):Introducing C#、Data Types、More with Data Types、Operators and Control Flow、Methods and Parameters。
- Type and object contracts(第6-11章):Classes、Inheritance、Interfaces、Value Types、Well-Formed Types、Exception Handling。
- Reusable abstraction(第12-18章):Generics、Delegates and Lambda Expressions、Events、Collection Interfaces with Standard Query Operators、LINQ with Query Expressions、Building Custom Collections、Reflection/Attributes/Dynamic。
- Execution and native boundaries(第19-21章):Multithreading、Thread Synchronization、Platform Interoperability and Unsafe Code。
- Runtime architecture(第22章):The Common Language Infrastructure。
切换官方22章的五个连续阶段
阶段一:第1-5章建立可预测的Language Model
第1章建立compiler、runtime与第一个program的边界;第2章讲primitive/reference/value representation、conversion与declaration;第3章扩展到null、tuple、array等data forms;第4章把values放入operators、expressions和control flow;第5章用methods、overload、optional/named/ref/out等参数建立call contract。
这五章的验收不是背关键字,而是给出source后能写出:每个expression的static type、conversion发生点、control path、哪个overload被选中、mutation是否回到caller。下面的method虽短,却同时验证null policy、parameter direction与failure channel。
public static bool TryNormalize(string input, out string normalized)
{
if (string.IsNullOrWhiteSpace(input))
{
normalized = null;
return false;
}
normalized = input.Trim().ToUpperInvariant();
return true;
}阶段交付物是一组table-driven cases:null、empty、whitespace、Unicode与normal input;每个case先写prediction,再运行assert。若解释依赖“value type总在stack、reference type总在heap”之类口号,应回到第2-3章用copy/identity/boxing和actual lifetime修正。
阶段二:第6-11章把Type做成可靠Contract
第6章从field/property/constructor/static/partial class建立invariant;第7章处理inheritance、virtual dispatch、abstract/sealed与base contract;第8章用interface表达capability与显式实现;第9章给struct/enum/value semantics边界;第10章集中检查object methods、equality、comparison、disposal与well-formed type要求;第11章定义exception creation、catch/rethrow/wrapping与diagnostics。
阶段项目可以是一个Money、Range或Session type。它必须阻止invalid construction,定义相等与hashing,明确是否有resource ownership,并让derived/consumer code无法破坏invariant。失败不是“代码不能编译”才算失败;mutable key进入dictionary后失联、Dispose重复释放、throw ex丢stack都说明contract未通过。
public sealed class Subscription : IDisposable
{
private readonly Action unsubscribe;
private bool disposed;
public Subscription(Action unsubscribe)
=> this.unsubscribe = unsubscribe ?? throw new ArgumentNullException(nameof(unsubscribe));
public void Dispose()
{
if (disposed) return;
disposed = true;
unsubscribe();
}
}阶段三:第12-18章组合Type、Behavior与Data Flow
第12章用generic parameters/constraints/variance复用type logic;第13章把method与captured state装进delegate/lambda;第14章给publisher独占raise权并处理subscription lifetime;第15章定义collection/enumerator和standard query operators;第16章解释query expression translation与provider;第17章实现indexer、iterator与custom collection protocol;第18章通过metadata、attributes、reflection与dynamic推迟部分binding。
这段必须沿execution timing学习。query construction不等于enumeration,event subscription不等于asynchronous dispatch,delegate invocation不等于new thread,reflection discovery不等于typed validation。读者要能指出谁保留closure/handler、每次enumeration是否重跑、OrderBy在哪里buffer、IQueryable由谁翻译。
IEnumerable<string> BuildNames(IEnumerable<User> users)
{
return users
.Where(user => user.IsActive)
.OrderBy(user => user.LastName)
.Select(user => user.DisplayName);
}
IReadOnlyList<string> snapshot = BuildNames(users).ToList();上例的return只构造deferred pipeline,ToList才建立snapshot。阶段验收再加一个custom iterator和event publisher:break时iterator finally执行,unsubscribe后subscriber可回收,query second enumeration的source mutation可预测。
阶段四:第19-21章管理Operation、Shared State与Native Memory
第19章区分Thread、ThreadPool、Task、async operation、parallelism与cooperative cancellation;第20章从shared invariant推导lock/Monitor/Interlocked/primitives、deadlock和Timer reentrancy;第21章越过managed boundary,核对DllImport ABI、marshalling、pointer/fixed与delegate callback lifetime。
这三章共同关注lifetime:operation何时完成,shared transaction何时恢复invariant,buffer/address和callback何时不再被native使用。阶段项目不是“开多个threads”,而是一个bounded importer或native callback wrapper,必须提供cancel/fault/dispose timeline、wait-for graph、buffer ownership表与in-flight drain测试。
选择岗位目标并生成chapter route
阶段五:第22章把Source放回CLI Execution Pipeline
第22章收束CLI、CTS、CLS、BCL、.NET Standard、assembly/manifest/module、CIL/metadata、JIT/AOT、GC和platform portability。它解释前21章产物如何被compiler表示、loader解析、runtime执行,也帮助区分compile error、missing API、assembly load、AOT reachability与native deployment故障。
阶段验收选择前面一个project,检查它的assembly identity、referenced assemblies、关键method CIL、reflection roots与target contract。不要把.NET Standard当runtime,也不要把assembly、DLL和module当同义词。
Evidence Loop:每章都要留下可检查产物
每章采用同一个五步循环:先预测行为,最小实现,检查compiler/runtime artifact,用contract语言解释,最后放进跨章case。只做笔记无法验证mental model;只复制代码也无法判断换一个input为何失败。
↡预测、实现、检查artifact、解释因果、跨章整合的重复学习闭环;每一步都有可观察输出。切换五类学习证据
建议节奏与完成定义
每章至少完成三个输出:一张自己的因果图、一段能改变输入的实验、一道failure-oriented answer。每阶段再做一个integrated artifact。完成不按阅读时长计,而按能否预测、实现、解释和迁移计。
- 第1-5章:语言行为trace与method contract tests。
- 第6-11章:well-formed domain type与failure/disposal tests。
- 第12-18章:collection/query/event pipeline与lifetime proof。
- 第19-21章:bounded/native operation与cancel/deadlock/ABI tests。
- 第22章:assembly/CIL/runtime artifact inspection与portability matrix。
本章回顾:22章是一条可验证Dependency Chain
- 官方22章是导航和覆盖的source of truth,五阶段只提供checkpoints。
- 章节可按目标加速,但hard prerequisite必须用证据通过。
- 每段输出行为、边界测试和解释,不能以“看完”代替掌握。
- 失败按compiler/type/protocol/concurrency/native/runtime层定位,走最短recovery route。
- 最终进入总复习,用综合系统同时验证type、behavior、lifetime与artifact。
练习
问题 1:目标是写高可靠library,最短路线怎样安排?
问题 2:会写LINQ链但无法解释结果何时变化,应怎样回补?
问题 3:P/Invoke在x86正常、x64失败,recovery route是什么?
术语表
名词解释
本章出现的专业名词,用大白话再讲一遍。
- official chapter spine
- prerequisite edge
- phase gate
- evidence loop
- recovery route