[Proposal]: Closed Hierarchies (VS 18.8, .NET 11)
- Dominant language
- C#
- Stars
- 12.7k
- Forks
- 1.1k
- Avg merge
- 11h 1m
- Merged PRs (30d)
- 3
Description
# Closed Hierarchies
* Specification: [Closed Hierarchies](https://github.com/dotnet/csharplang/blob/main/proposals/csharp-15.0/closed-hierarchies.md)
* Working group folder: [Discriminated Unions](https://github.com/dotnet/csharplang/tree/main/meetings/working-groups/discriminated-unions)
* Discussion: #8927
## Summary
Allow a class to be declared `closed`. This prevents directly derived classes from being declared in a different assembly:
``` c#
// 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 class
```
A 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.
``` c#
// 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
* https://github.com/dotnet/csharplang/issues/485
* https://github.com/dotnet/csharplang/discussions/8927
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.