Search⌘ K
AI Features

Enumerations

Explore how to define and use enumerations in C# to represent sets of related constants like days of the week. Learn the syntax, assign values, and apply enums for type safety and clearer code, enabling you to handle specific states and parameters effectively in your programs.

C# allows us to create enumerations in addition to primitive data types. An enumeration in C# represents a collection of logically related constants.

Suppose a method needs to accept a day of the week as a parameter. We have several options:

  • We could accept a string value (like "Monday").

  • We could accept an integer that represents the day of the week (like 2).

  • We could use enumerations.

Enumerations definition

We can create an enumeration containing all days of the week to ensure the caller selects a valid, predefined constant.

An enumeration is declared using the enum keyword:

enum WeekDay
{
}
Declaring an empty enumeration
  • Line 1: Uses the enum keyword to define a new type named WeekDay.

We can optionally specify the underlying type of the enumeration, which must be an integral type. If the type is omitted, int is used by default.

enum WeekDay : short
{
}
Specifying the underlying type
  • Line 1: Declares the WeekDay enum and specifies short as the underlying type.

The body of the enumeration consists of comma-separated constants:

enum WeekDay : short
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Defining enum members
  • Lines 3–9: List the named constants representing the days of the week.

Each element of the enumeration is assigned an integer value, with the first element valued at 0, the second element 1, and so on.

We can specify the elements’ values by indicating the first value:

enum WeekDay : short
{
Monday = 1,
Tuesday, // 2
Wednesday, // 3
Thursday, // 4
Friday, // 5
Saturday, // 6
Sunday // 7
}
Customizing the starting value
  • Line 3: Explicitly assigns 1 to Monday. Subsequent members increment automatically.

We can also specify the values of all constants:

enum YearCount
{
Year = 1,
Decade = 10,
Century = 100,
Millennium = 1000
}
Shared and referenced values
  • Lines 4–5: Assign the value 2 to Rock and Alternative.

  • Line 6: Assigns the value of Classic (which is 1) to Old.

Enumerations usage

Each enum defines a new data type. This means that we can declare a variable of this type and use it. Let’s try it out with an example:

C# 14.0
enum WeekDay : short
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
  • Line 2: Declares a variable currentDay of type WeekDay and assigns it the constant Wednesday.

  • Line 4: Prints the name of the constant (“Wednesday”) to the console.

  • Lines 7–16: Defines the WeekDay enumeration at the bottom of the file.

Note: It’s good practice to have a separate file for each type we create. In a real-world application, the WeekDay enum would likely reside in its own file (e.g., WeekDay.cs) to keep the codebase organized.

After creating an enumeration type, we can create a variable of this type and assign it a value. Note that we can only assign one enumeration constant to this variable. Although these constants are backed by integers internally, we cannot assign a number directly to an enumeration variable.

C# 14.0
// Error: Cannot implicitly convert type 'int' to 'WeekDay'
WeekDay currentDay = 2;
Console.WriteLine("Cannot assign integer 2 directly to WeekDay variable.");
  • Line 2: Represents the attempted assignment. If uncommented, this would cause a compilation error because C# enforces type safety.

The error hints that we need an explicit cast. When we print the variable of the enum type, we get the string with the constant’s name instead of its numeric value. If we need the numeric value, we can use an explicit cast:

C# 14.0
WeekDay currentDay = WeekDay.Wednesday;
Console.WriteLine($"Today is {(int)currentDay}");
  • Line 3: Uses (int) to explicitly cast the WeekDay value to its integer representation. This prints "2" (since Monday is 0, Tuesday is 1, Wednesday is 2).

Enumerations in practice

We typically use enumerations to represent state, performing different actions based on that state. We could use enums as parameters of methods when we need to have only specific input for a method.

It’s good practice to have a separate file for each type we create. That’s why we’ve defined our enumeration inside a different file here.

Consider the following example:

C# 14.0
WeekDay currentDay = WeekDay.Wednesday;
PrintMessageOfTheDay(currentDay);
// Here our method accepts an enum type as a parameter
void PrintMessageOfTheDay(WeekDay day)
{
// Performing different operations based on the value of the enum variable
switch (day)
{
case WeekDay.Monday:
Console.WriteLine("Brace yourself. The week only started.");
break;
case WeekDay.Tuesday:
Console.WriteLine("Still many days to go...");
break;
case WeekDay.Wednesday:
Console.WriteLine("Halfway through. Great!");
break;
case WeekDay.Thursday:
Console.WriteLine("Just one more day left!");
break;
case WeekDay.Friday:
Console.WriteLine("Ohooo! Party tonight!");
break;
case WeekDay.Saturday:
Console.WriteLine("Time to enjoy the weekend.");
break;
case WeekDay.Sunday:
Console.WriteLine("The last day...");
break;
}
}
  • Line 1: Creates a variable and passes it to a method.

  • Line 6: Defines a local method PrintMessageOfTheDay that takes the enum as a parameter.

  • Line 9: Uses a switch statement to execute different logic based on the enum value.

Note: When using top-level statements in a single file, we must write our executable code (like assignments and printing) before defining the enum.