Skip to main content

Enumerations enum

In addition to primitive data types, the C# programming language has such a type as enum or enumeration. Enumerations represent a set of logically related constants.

An enum is declared using the enum operator:

enum enum name_enumeration
{
// enumeration values
value1,
value2,
.......
valueN
}

After the enum operator comes the name of the enumeration. And then the enumeration constants are listed in curly brackets, comma separated.

Let's define the simplest enumeration:

enum DayTime
{
Morning,
Afternoon,
Evening,
Night
}

The DayTime enumeration is defined here, which has four values: Morning, Afternoon, Evening, and Night

Each enumeration actually defines a new data type with which we can define variables, constants, method parameters, etc. just as we can with any other type. The value of a variable, constant and method parameter that represent an enumeration must be one of the constants of that enumeration, for example:

const DayTime dayTime = DayTime.Morning;

Further in the program, we can use such variables/constants/parameters like any other:

DayTime dayTime = DayTime.Morning;

if(dayTime == DayTime.Morning)
Console.WriteLine(“Good morning”);
else
Console.WriteLine(“Hello”);

enum DayTime
{
Morning,
Afternoon,
Evening,
Night
}

State Storage

Often an enumeration variable acts as a state store, depending on which some actions are performed:

DayTime now = DayTime.Evening;

PrintMessage(now); // Good evening
PrintMessage(DayTime.Afternoon); // Good afternoon
PrintMessage(DayTime.Night); // Good night

void PrintMessage(DayTime dayTime)
{
switch (dayTime)
{
case DayTime.Morning:
Console.WriteLine(“Good morning”);
break;
{ case DayTime.Afternoon:
Console.WriteLine(“Good afternoon”);
break;
case DayTime.Evening:
Console.WriteLine(“Good evening”);
break;
case DayTime.Night:
Console.WriteLine(“Goodnight”);
break;
}
}
enum DayTime
{
Morning,
Afternoon,
Evening,
Night
}

Here, the PrintMessage() method takes a value of enum type DayTime as a parameter and depending on this value, it outputs a certain prefix.

Another example:

DoOperation(10, 5, Operation.Add); // 15
DoOperation(10, 5, Operation.Subtract); // 5
DoOperation(10, 5, Operation.Multiply); // 50
DoOperation(10, 5, Operation.Divide); // 2

void DoOperation(double x, double y, Operation op)
{
double result = op switch
{
Operation.Add => x + y,
Operation.Subtract => x - y,
Operation.Multiply => x * y,
Operation.Divide => x / y
};
Console.WriteLine(result);
}
enum Operation
{
Add,
Subtract,
Multiply,
Divide
}

The Operation enumeration is defined here to represent arithmetic operations. Each operation type is defined as one of the constants of the enumeration. And also the method DoOperation() is defined, which takes two numbers and the operation type as parameters in the form of an enumeration constant and depending on this type returns the result of the defined operation from the switch construct.

Type and values of enumeration constants

Enumeration constants can have a type. The type is specified after the enumeration name with a colon:

enum Time : byte
{
Morning,
Afternoon,
Evening,
Night
}

The enum type must necessarily represent an integer type (byte, sbyte, short, ushort, int, uint, long, ulong). If no type is explicitly specified, the default type is int.

The type affects the values that constants can have. By default, each element of an enumeration is assigned an integer value, with the first element having a value of 0, the second element having a value of 1, and so on. For example, take the above defined DayTime:

DayTime now = DayTime.Morning;

Console.WriteLine((int) now); // 0
Console.WriteLine((int) DayTime.Night); // 3

enum DayTime
{
Morning,
Afternoon,
Evening,
Night
}

We can use the cast operation to get the integer value of an enumeration constant:

(int) DayTime.Night // 3

At the same time, even though each constant is mapped to a specific number, we can NOT assign a numeric value to it:

DayTime now = 2; // ! Error

We can also explicitly specify the values of the elements, either by specifying the value of the first element:

enum DayTime
{
Morning = 3, // every next element is incremented by one by default
Afternoon, // this element is 4
Evening, // 5
Night // 6
}

But you can also explicitly specify values for all elements:

enum DayTime
{
Morning = 2,
Afternoon = 4,
Evening = 8,
Night = 16
}

The enumeration constants can have the same values, or you can even assign one constant to the value of another constant:

enum DayTime
{
Morning = 1,
Afternoon = Morning,
Evening = 2,
Night = 2
}