Skip to content

[Proposal]: Closed Enums #9011

@333fred

Description

@333fred

Closed Enums

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

Related Proposals

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions