Suggestions to improve this closed hierarchies tutorial experience by enhancing the code snippets

オープン 初心者向け
#57,026 コメント 2 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@natandovale がすでに取り組んでいます。

2026年11月30日 から。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
ドキュメント
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
documentation

調査の方向性

issueで指定されたコンテンツソースである docs/csharp/whats-new/tutorials/closed-hierarchies.md を編集します。「Declare a closed hierarchy」「Decide what to seal」「Use a closed hierarchy with generics」「Run the sample」の各セクションを読み、要望どおりにコードスニペットを更新してください。外側のクラスを示し、重複したコードを削除します。チュートリアルのコードスニペットが提案A~Dに一致し、読者がメソッドの配置先を推測せずに読み進められる状態になれば完了です。

索引モデルが issue の本文から書いたものです。

説明

dotnet-csharp/svc whats-new/subsvc
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

Related Issues

主要言語
言語のデータがありません
スター
4.8k
フォーク
6.1k
平均マージ
1日 3時間
マージ済み PR(30日)
263

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/docs のほかの issue

dotnet/docs の issue をすべて見る

似ている issue

Documentation の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。