Suggestions to improve this closed hierarchies tutorial experience by enhancing the code snippets
Maintainer thường phản hồi trong vòng 1 ngày
@natandovale đang làm issue này rồi.
Từ ngày 30/11/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- csharp
- Lĩnh vực
- documentation
Hướng nghiên cứu
Chỉnh sửa docs/csharp/whats-new/tutorials/closed-hierarchies.md, nguồn nội dung được nêu trong issue. Đọc các phần “Declare a closed hierarchy”, “Decide what to seal”, “Use a closed hierarchy with generics” và “Run the sample”, sau đó cập nhật các đoạn mã theo yêu cầu: hiển thị các lớp bao quanh và xóa mã trùng lặp. Hoàn tất khi các đoạn mã trong hướng dẫn khớp với các đề xuất A–D và người đọc có thể theo dõi mà không cần suy đoán các phương thức thuộc về đâu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Không có dữ liệu ngôn ngữ
- Star
- 4.8k
- Fork
- 6.1k
- Merge trung bình
- 1 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 263
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/docs
-
:watch: Not Triaged dotnet-target-version
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 80/100
Maintainer thường phản hồi trong vòng 1 ngày
-
"Supported target frameworks" lists net48 for the .NET SDK, but dotnet build can't build some net48 projectsCó thể đã có người làm @BillWagner đã nhận hôm nay. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 85/100
dotnet/docs#57038 · 1 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
TargetFramework doesn't match surrounding text ("net11.0" vs ".NET 9")Có thể đã có người làm @BillWagner đã nhận 5 ngày trước. Đang mởdotnet-fundamentals/svc
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
dotnet/docs#57011 · 1 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`--self-contained false` doesn't seem to be a valid flagCó thể đã có người làm @BillWagner đã nhận 7 ngày trước. Đang mởdotnet-fundamentals/svc
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
dotnet/docs#56970 · 1 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
redirect-article deletes the source article before creating the redirect entryCó thể đã có người làm @BillWagner đã nhận 10 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
dotnet/docs#56948 · 1 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Cojekt/LibreDental#258 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
marketing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
NuSkooler/enigma-bbs#1241 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
next
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
j3ko/agent-plugins#80 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Relatório semanal de 01/12/2026Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
aluiziodeveloper/portfolio-vivo-gravacao#51 ·
Maintainer thường phản hồi trong vòng 1 ngày