Information Technology Reference
In-Depth Information
Enumerations
An enumeration, or enum, is a programmer-defined type, like a class or a struct.
Like structs, enums are value types, and therefore store their data directly, rather than
separately, with a reference and data.
￿
Enums have only one type of member: named constants.
For example, the following code shows the declaration of a new enum type called
TrafficLight , which contains three members. Notice that the list of member declarations
is a comma-separated list; there are no semicolons in an enum declaration.
Keyword Enum name
enum TrafficLight
{
Green, Comma separated--no semicolons
Yellow, Comma separated--no semicolons
Red
}
The following code shows the declaration of three variables of the enum type
TrafficLight , which was just defined in the preceding code. Notice that you can assign
member literals to variables, or you can copy the value from another variable of the same type.
class Program {
static void Main()
{ Type Variable Member
TrafficLight t1 = TrafficLight.Red; // Assign from member
TrafficLight t2 = TrafficLight.Green; // Assign from member
TrafficLight t3 = t2; // Assign from variable
Console.WriteLine(t1);
Console.WriteLine(t2);
Console.WriteLine(t3);
}
}
This code produces the following output. Notice that the member names are printed
as strings.
Red
Green
Green
Search WWH ::




Custom Search