In many ways, Enums are implemented as syntactic sugar in .NET languages. We quote
The enum keyword is used to declare an enumeration, a separate type consisting of a set of named constants called a list of enumerators.
In other words, enumeration values ββare fields, like any other, and not an integral choice of values. There is nothing to prevent you from listing invalid values ββin an enumeration. The most you can do to force the actual contents of an enumeration to use the static Enum.IsDefined
method (although you can define extension methods if your imagination requires it).
However, the purpose of enumerations is not to check user input, so they should not be as strict as this. The purpose of enumerations is to associate a name with a numerical value and easily display a list of possible combinations of this type. And don't forget that their free design allows things like Day.Saturday | Day.Friday
Day.Saturday | Day.Friday
or more complex couplings. They are still very helpful.
source share