-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
Closed Enums
- Specification: Closed Enums
- Working group folder: Discriminated Unions
- Discussion: Proposal: "Closed" enum types #9010
Summary
Allow an enum type to be declared closed:
public closed enum Color
{
Red,
Green,
Blue
}This prevents creation of enum values other than the specified members.
A consuming switch expression that covers all its specified members can therefore be concluded to "exhaust" the closed enum - it does not need to provide a default case to avoid warnings:
Color color = ...;
string description = color switch
{
Red => "red",
Green => "green",
Blue => "blue"
// No warning about missing cases
};Design meetings
- https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-09-26.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-enums
Related Proposals
Reactions are currently unavailable