Repository navigation
Conversation
|
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. |
There was a problem hiding this comment.
🟡 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
| public class Circle(double Radius) : Shape; | ||
| public class Rectangle(double Width, double Height) : Shape; |
There was a problem hiding this comment.
Added explicit public properties initialized from the primary constructor parameters.
BillWagner
left a comment
There was a problem hiding this comment.
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 theclosed recordtypes.) - 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. |
There was a problem hiding this comment.
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:
- It's a breaking runtime and compile time change.
- If dependent code isn't recompiled, any impacted
switchexpressions couldthrow. If the dependent code is recompiled, the compiler issues a warning.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Applied: rewrote this section to define closed hierarchies directly as a polymorphic base type providing shared behavior across known subtypes without negative definitions.
|
@BillWagner I have updated the explanations and examples across the articles to emphasize the distinction between direct subtypes and leaf types:
|

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