logo
Учебник_ПОА

Enumerations

C# enables you to create your own set of named constants using the enum keyword. These data types allow you to declare a set of names or other literal values that define all the possible values that can be assigned to a variable.

For example, if your program deals with the days of the week, you might want to create a new type called DayOfWeek. You could then declare a new variable of DayOfWeek type, and assign it a value. Using this data type allows your code to be more readable, and also makes it less likely that an illegal or unexpected value will be assigned to the variable.

public enum DayOfWeek

{

Sunday = 0,

Monday = 1,

Tuesday = 2,

Wednesday = 3,

Thursday = 4,

Friday = 5,

Saturday = 6

}

class Program

{

static void Main()

{

DayOfWeek day = DayOfWeek.Monday;

int i = (int) DayOfWeek.Monday;

System.Console.WriteLine(day); // displays Monday

System.Console.WriteLine(i); // displays 1

}

}