Enumerations
An enumeration is a set of named integral constants. We use the enum
keyword to declare an enumeration. An enumeration is a value type. Enumerations are useful when we want to use a limited number of integral values for some particular purpose. Defining and using an enumeration has several advantages:
- We use named constants instead of literal values. This makes the code more readable and easier to maintain.
- When you use IDEs, such as Visual Studio, you can see the list of possible values that can be assigned to a variable.
- It enforces type safety for using numerical constants.
The following example shows an enumeration called Priority
with four possible values:
enum Priority { Low, Normal, Important, Urgent }
Each element of an enumeration stands for an integer value. By default, the first identifier is assigned to zero (0
). The value of each successive...