Tip:
Highlight text to annotate it
X
An enumeration is a special kind of value type consisting of a list of named constants.
To create one, the enum keyword is used followed by a name and a code block,
containing a comma-separated list of constant elements.
This enumeration type can be used to create variables that can hold these constants.
To assign a value to the enum variable, the elements are accessed from the enum as if they were static members of a class.
The switch statement provides a good example of when an enumeration can be useful.
Compared to using ordinary constants, an enumeration has the advantage of allowing
the programmer to clearly specify what constant values are allowed.
This provides compile-time type safety, and IntelliSense also makes the values easier to remember.
There is usually no need to know the actual constant values that the constants represent, but sometimes it may be necessary.
By default, the first element has the value 0, and each successive element has one value higher.
These default values can be overridden by assigning values to the constants.
The values can be computed, and do not have to be unique.
The underlying type of the constant elements is implicitly specified as int,
but this can be changed by using a colon after the enumeration's name, followed by the desired integer type.
The access levels for enumerations are the same as for classes.
They are internal by default, but can also be declared as public.
Although enumerations are usually defined at the top-level, they may be contained within a class.
In a class they have private access by default, and can be set to either one of the access levels.
An enumeration constant can be cast to an int, and the ToString method can be used to obtain its name.
Most other enumeration methods can be found in the System.Enum class.