Suggestions to improve this closed hierarchies tutorial experience by enhancing the code snippets
维护者通常 1 天内回复
@natandovale 已经在做这个了。
开始于 2026年11月30日。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 82/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
调研方向
编辑 docs/csharp/whats-new/tutorials/closed-hierarchies.md,这是 issue 中指定的内容源。阅读“Declare a closed hierarchy”、“Decide what to seal”、“Use a closed hierarchy with generics”和“Run the sample”部分,然后按要求更新代码片段:展示外围类并删除重复代码。完成标准是教程中的代码片段符合建议 A–D,读者无需推断方法应放在哪里就能读懂。
由索引模型根据 Issue 内容生成。
描述
Type of issue
Missing information
Description
I'm the editor of the Ecma C# standard, and I'm writing a formal spec for closed hierarchies for that standard. To learn about this feature, I started with this tutorial. While it is very good and provides all the info needed, the reader has to figure out a few things, which could be eliminated by a small amount of tweaking of some code snippets. Specifically:
Suggestion A
In section "Declare a closed hierarchy," item 2 states, "In the same file, add a method that matches every sensor with a switch expression:" followed by the code snippet
public static string Describe(Sensor sensor) => sensor switch
{
Temperature temperature => $"{temperature.Celsius:F1}°C",
Humidity humidity => $"{humidity.Percent:F0}% RH",
Contact contact => contact.Open ? "open" : "closed",
// No default arm is needed. Sensor is closed, so these cases are exhaustive.
};
Now as a method has to be declared inside a class, which class would that be? One of the 4 just declared? A new class? As it happens, we later learn from the App code that it is expected to be a new public class called SensorReporter. As such, I propose you enhance the method code snippet to be the following:
public class SensorReporter
{
public static string Describe(Sensor sensor) => sensor switch
{
Temperature temperature => $"{temperature.Celsius:F1}°C",
Humidity humidity => $"{humidity.Percent:F0}% RH",
Contact contact => contact.Open ? "open" : "closed",
// No default arm is needed. Sensor is closed, so these cases are exhaustive.
};
}
Suggestion B
In section "Decide what to seal," item 2 states, "In SmartHome.App, add code that matches a DoorContact through the existing Sensor switch:" followed by the code snippet
// A DoorContact from another assembly still matches the Contact case.
Sensor frontDoor = new DoorContact(Open: false, Door: "Front");
Console.WriteLine($"Sensor: {SensorReporter.Describe(frontDoor)}");
I propose you enhance the method code snippet to be the following:
public class Program
{
public static void Main()
{
// A DoorContact from another assembly still matches the Contact case.
Sensor frontDoor = new DoorContact(Open: false, Door: "Front");
Console.WriteLine($"Sensor: {SensorReporter.Describe(frontDoor)}");
}
}
Suggestion C (a clone of Suggestion A)
In section "Use a closed hierarchy with generics," item 2 states, "Add a recursive method that accumulates the report by switching over its cases" followed by the code snippet
public static int Count<T>(Report<T> report) => report switch
{
Single<T> => 1,
Group<T> group => Count(group.Left) + Count(group.Right),
// Exhaustive: Report<T> is closed and both subtypes are handled.
};
Again, this method needs to be inside a new class, which the App code expects to be called ReportReporter. As such, I propose you enhance the method code snippet to be the following:
public class ReportReporter
{
public static int Count<T>(Report<T> report) => report switch
{
Single<T> => 1,
Group<T> group => Count(group.Left) + Count(group.Right),
// Exhaustive: Report<T> is closed and both subtypes are handled.
};
}
Suggestion D
In section "Run the sample," item 2 states, "Open Program.cs and add the following code:" followed by a code snippet that includes the following:
// A DoorContact from another assembly still matches the Contact case.
Sensor frontDoor = new DoorContact(Open: false, Door: "Front");
Console.WriteLine($"Sensor: {SensorReporter.Describe(frontDoor)}");
As that code is already in App's Main there is no need to include it again here; just remove this duplicate from here.
Bottom Line
Making these small enhancements will allow every reader to copy/paste the code snippets and have things work "right out of the box!"
Page URL
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/closed-hierarchies
Content source URL
https://github.com/dotnet/docs/blob/main/docs/csharp/whats-new/tutorials/closed-hierarchies.md
Document Version Independent Id
52e2d8e7-937a-31a8-5652-5bfac9d52322
Platform Id
c9a713b9-fc32-2a98-767f-43aae7fefa51
Article author
@BillWagner
Metadata
- ID: 6b17fb8f-f060-8e1b-252f-7eb7961cdaf2
- PlatformId: c9a713b9-fc32-2a98-767f-43aae7fefa51
- Service: dotnet-csharp
- Sub-service: whats-new
- 主要语言
- 没有语言数据
- 星标
- 4.8k
- 派生
- 6.1k
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 263
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/docs 的其他 Issue
-
:watch: Not Triaged dotnet-target-version
难度 1/5 1 小时以内 新手友好度 80/100
维护者通常 1 天内回复
-
"Supported target frameworks" lists net48 for the .NET SDK, but dotnet build can't build some net48 projects可能已有人在做 @BillWagner 今天认领。 未关闭
难度 1/5 1-3 小时 新手友好度 85/100
dotnet/docs#57038 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
-
TargetFramework doesn't match surrounding text ("net11.0" vs ".NET 9")可能已有人在做 @BillWagner 于 5 天前认领。 未关闭dotnet-fundamentals/svc
难度 1/5 1 小时以内 新手友好度 88/100
dotnet/docs#57011 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
-
`--self-contained false` doesn't seem to be a valid flag可能已有人在做 @BillWagner 于 7 天前认领。 未关闭dotnet-fundamentals/svc
难度 1/5 1 小时以内 新手友好度 82/100
dotnet/docs#56970 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
-
redirect-article deletes the source article before creating the redirect entry可能已有人在做 @BillWagner 于 10 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 67/100
dotnet/docs#56948 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
Cojekt/LibreDental#258 ·
维护者通常 1 天内回复
-
marketing
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 88/100
NuSkooler/enigma-bbs#1241 ·
维护者通常 1 天内回复
-
next
难度 2/5 1-3 小时 新手友好度 75/100
j3ko/agent-plugins#80 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 75/100
aluiziodeveloper/portfolio-vivo-gravacao#51 ·
维护者通常 1 天内回复