[Proposal]: Closed Hierarchies (VS 18.8, .NET 11)
Author: mattwarCreated Jun 30, 2025Updated Aug 12, 2026
LabelsProposal championImplemented Needs ECMA Spec
Closed Hierarchies
- Specification: Closed Hierarchies
- Working group folder: Discriminated Unions
- Discussion: #8927
Summary
Allow a class to be declared closed. This prevents directly derived classes from being declared in a different assembly:
// Assembly 1
public closed record class GateState;
public record class Closed : GateState;
public record class Open(float Percent) : GateState;
// Assembly 2
public record class Locked : GateState; // ERROR - 'GateState' is a closed classA consuming switch expression that covers all those derived classes can therefore be concluded to "exhaust" the closed class - it does not need to provide a default case to avoid warnings.
// Assembly 3
GateState state = ...;
string description = state switch
{
Closed => "closed",
Open(var percent) => $"{percent}% open"
// No warning about missing cases
}; Design meetings
- https://github.com/dotnet/csharplang/blob/master/meetings/2017/LDM-2017-01-10.md
- https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-08-31.md#discriminated-unions
- https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-26.md#discriminated-unions
- https://github.com/dotnet/csharplang/blob/main/meetings/2024/LDM-2024-07-24.md#discriminated-unions
- https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-06-25.md#unions
- https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-10-01.md#closed-hierarchies
- https://github.com/dotnet/csharplang/blob/main/meetings/2026/LDM-2026-02-09.md#closed-hierarchies-open-questions
- https://github.com/dotnet/csharplang/blob/main/meetings/2026/LDM-2026-04-20.md#closed-hierarchies-continued
- https://github.com/dotnet/csharplang/blob/main/meetings/2026/LDM-2026-05-18.md#closed-hierarchies-open-questions
Related Proposals
Source: dotnet/csharplang