Enumerations
An enumeration can give a specific range of numbers a name.
The structure of an enumeration:
<enum_value> ::= <number>
<enum_member> ::= <name> ("=" <enum_value>)?
<enum_declaration> ::= <modifiers> "enum" <name> "{" <enum_member> "}"
A simple example:
public enum ConsoleColor {
Red = 0,
Green = 1,
Blue = 2
}
If you leave out the value the compiler assigns the value automatically based on the index.
Exercises
Declare an enum for representing the gender of a person and write a
Person
class with a gender field.What can enums be used for?
Which values can hold an enum?
Last updated