Enumerations in .NET
- Last Updated: January 17, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
Enumerations in .NET
In .NET, an enumeration is a special kind of value
type object that inherits from System.Enum and
whose members correspond to a set of constant values with a common
underlying primitive data type. Each member is denoted by a unique
name, analogous to a property of a class. For example, in .NET you
might have a Color enumeration defined in a MyGraphics namespace
with a common underlying integer data type, where the value, 5, is
denoted by the enumeration member, Blue. In .NET, you
reference members of an enumeration similar to members of any other .NET class,
for example (in .NET notation), MyGraphics.Color.Blue.
Each .NET language provides its own syntax for defining enumeration types and accessing their values (members). This includes operator overloading that allows enumeration data items to be manipulated like any other data item with the same underlying primitive data type. Typically, this means that, in .NET, if you define a variable with an enumeration type, you can assign and evaluate (with casting) a compatible value of the same primitive data type to the enumeration variable.
For example, in .NET, if you define a variable with the Color enumeration
type, you can assign the enumeration member, Blue to
that variable, or you can cast and assign the integer value, 5,
to the same variable, both of which are equivalent. You can also
perform binary operations, such as addition and subtraction, as
defined for the enumeration and its underlying data type. For example,
you might be able to add two enumeration members together to obtain
the value of a third, as in the expression MyGraphics.Color.Blue
+ MyGraphics.Color.Yellow.
Therefore, as a value type in .NET, an enumeration type can function interchangeably with its underlying primitive data type.