Skip to content

Add fundamentals coverage for C# 15 closed hierarchies - #56517

Open
SkyDevLab wants to merge 5 commits into
dotnet:mainfrom
SkyDevLab:docs/54014-closed-hierarchies
Open

SkyDevLab wants to merge 5 commits into
dotnet:mainfrom
SkyDevLab:docs/54014-closed-hierarchies

Conversation

@SkyDevLab

@SkyDevLab SkyDevLab commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Adds C# 15 (.NET 11) fundamentals coverage for closed hierarchies across classes, records, type patterns, and the type system overview.

Fixes #54014


Internal previews

File Preview link
docs/csharp/fundamentals/patterns/type-patterns.md Learn preview
docs/csharp/fundamentals/types/classes.md Learn preview
docs/csharp/fundamentals/types/index.md Learn preview
docs/csharp/fundamentals/types/records.md Learn preview

Build report

@SkyDevLab
SkyDevLab requested review from a team and BillWagner as code owners October 21, 2026 17:58
@dotnetrepoman dotnetrepoman Bot added this to the October 2026 milestone Oct 21, 2026
@dotnet-policy-service dotnet-policy-service Bot added dotnet-csharp/svc fundamentals/subsvc community-contribution Indicates PR is created by someone from the .NET community. labels Oct 21, 2026
@BillWagner
BillWagner requested a balanced review from Copilot October 22, 2026 14:16
@BillWagner

Copy link
Copy Markdown
Member

Hi @SkyDevLab

I'm going to hold this for now. I had planned on this for November because the Fundamentals section is tailored for developers new to C#. A very small slice of those readers download the preview releases (including the RC builds).

I'll plan on merging and publishing around the GA release for .NET 11.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new shape-matching example accesses properties that its classes don't define, causing compilation errors.

1 open issue
What changed in this PR

Adds C# 15 (.NET 11) closed-hierarchy coverage to the fundamentals documentation, addressing #54014 across classes, records, type selection, and pattern matching.

Changes:

  • Explains closed class and record hierarchies and compares them with union types.
  • Adds examples of exhaustive pattern matching over closed hierarchies.
  • Updates the type-system overview with closed-hierarchy guidance.
File Description
docs/​csharp/​fundamentals/​types/​records.md Adds closed record hierarchy and result-matching examples.
docs/​csharp/​fundamentals/​types/​index.md Adds closed hierarchies to class and type-selection guidance.
docs/​csharp/​fundamentals/​types/​classes.md Explains closed classes with a payment-method example.
docs/​csharp/​fundamentals/​patterns/​type-patterns.md Adds exhaustive shape matching and subtype-change guidance.

Review effort: Balanced

Comment on lines +53 to +54
public class Circle(double Radius) : Shape;
public class Rectangle(double Width, double Height) : Shape;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added explicit public properties initialized from the primary constructor parameters.

@BillWagner BillWagner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the specific comments, these apply throughout the modified articles.

There are misleading subtleties in the explanations of closed hierarchies.

The phrase "the compiler knows every possible direct subtype" is correct, but readers can interpret it incorrectly. The direct qualifier is de-emphasized. Let's rework how this is explained, and add a number of illustrative examples:

  • If all the direct subtypes are sealed, then the hierarchy is closed to all derivation. (This would be a great addition below with the closed record types.)
  • If the direct subtypes aren't all sealed, the closed hierarchy represents all subtypes, but not all possible leaf types.

Your explanations follow that format throughout. they can all be easily misinterpreted by readers new to C#.

};
```

If a new subtype is added to the closed hierarchy in the future, the compiler issues a warning wherever an existing `switch` expression is missing the new case, preventing unhandled runtime cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also misleading: It is true only if a new direct subtype is added to the closed hierarchy. This should also emphasize two related points:

  1. It's a breaking runtime and compile time change.
  2. If dependent code isn't recompiled, any impacted switch expressions could throw. If the dependent code is recompiled, the compiler issues a warning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied: clarified that adding a new direct subtype to a closed hierarchy is both a compile-time (compiler warning on recompiled dependent code) and runtime (throws SwitchExpressionException if dependent code is not recompiled) breaking change.

};
```

A `closed` class is implicitly abstract and cannot be directly instantiated. Unlike [union types](../../language-reference/builtin-types/union.md), which define a fixed union across distinct value or reference types, closed hierarchies use standard object-oriented inheritance and reference polymorphism. Subtypes can share base implementations, declare virtual members, and encapsulate their own fields and behavior.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is muddled.

First, don't define something by what it isn't. There are a lot of things that closed isn't. Tell readers what it is. The fact that the closed class is abstract isn't related to the other sentences in this paragraph. Think through what concept you're trying to explain here, and write to clearly explain it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied: rewrote this section to define closed hierarchies directly as a polymorphic base type providing shared behavior across known subtypes without negative definitions.

@SkyDevLab

Copy link
Copy Markdown
Contributor Author

@BillWagner I have updated the explanations and examples across the articles to emphasize the distinction between direct subtypes and leaf types:

  1. classes.md: Added a dedicated Direct subtypes vs. leaf types section illustrating:
    • When all direct subtypes are sealed, the hierarchy is completely closed to all derivation.
    • When a direct subtype is unsealed (e.g. CreditCard extended by RewardsCreditCard), the hierarchy defines all direct subtypes, but not all possible leaf types. Pattern matching on CreditCard covers both CreditCard and RewardsCreditCard.
  2. **
    ecords.md**: Updated the Result example to declare sealed record class Success and sealed record class Failure, explaining how sealed closes the hierarchy against further derivation.
  3. ** ype-patterns.md**: Clarified that closed governs direct derivation, that marking direct subtypes sealed closes the hierarchy to all derivation, and that matching a non-sealed direct subtype also matches any of its derived subtypes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc fundamentals/subsvc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C# 15-Fundamentals]: New Feature - Closed hierarchies

3 participants