How to Use Enums in C#

Enums are a useful data type that let you define a list of named options for a variable. For example, if you want to store each day of the week, you can use an enum to represent the valid values.

Enums make your code easier to read and write by encouraging meaningful names and limiting the values you can use. It’s often convenient to use a switch statement to handle the possible values of an enum.

You can declare and use enums in many programming languages, including C#.

How to Declare an Enum in C#

In C#, you’ll usually decalre enums at the namespace level. For example, in a C# console application, you can declare your enums as class properties. You can view an example in this GitHub repo.

class Program
{
  enum Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
  }

  static void Main(string[] args)
  {
            
  }
}

When you declare an enum, each option gets an integer value to represent its index. By default, the first value in the enum has an index value of 0. To change this, you can specify your own index for the first element when you declare the enum:

enum Weekday { 
  Monday = 1,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

In this case, the index of the first element Monday starts at 1. The index of the next element Tuesday will be 2, and so on.

How to Access an Enum Value in C#

There are many different ways you can access the values stored inside enums. The value returned for each option in the enum is the index. For this example, the value of Weekday.Friday is 5.

One way you can retrieve the name of the enum is by using the Enums.GetName() function. This gets the name of the option at index 5:

string friday = Enum.GetName(typeof(Weekday), Weekday.Friday);
Console.WriteLine("Thank God it's " + friday + "!");
// Output = Thank God it's Friday!

You can also access a value using its index. In this case, the value at index 2 in the Weekday enum (which is Tuesday) will get stored in the variable:

Weekday tuesday = (Weekday)2;
Console.WriteLine("Today is " + tuesday);
// Output = Today is Tuesday

When you pass an enum as an argument into Console.WriteLine(), the toString() method for the enum runs automatically. This means that instead of printing the true numerical value of the enum, which is 5, it will print the string value, Friday.

Console.WriteLine(Weekday.Thursday + " is the new " + Weekday.Friday);
// Output = Thursday is the new Friday

You can also convert or parse the data type into an integer as another way to store an option's index:

int mondayIndex = (int)Weekday.Monday;
Console.WriteLine(mondayIndex);
// Output = 1

How to Compare Enums in C#

In C#, you can compare enums using the comparison operators (==, !=, <, >, <=, >=). This will compare the numeric value of the options.

Weekday day1 = Weekday.Monday;
Weekday day2 = Weekday.Tuesday;

if (day1 == day2)
{
  Console.WriteLine("The days are the same.");
}
else
{
  Console.WriteLine("The days are different.");
}

In this example, the value of Weekday.Monday is 1, and the value of Weekday.Tuesday is 2, so the 1 == 2 comparison will fail.

Another way of comparing enums is the Equals() method:

if (day1.Equals(day2))
{
  Console.WriteLine("The days are the same.");
}
else
{
  Console.WriteLine("The days are different.");
}

You can also use a C# switch statement to compare a value against each option in an enum. In this example, the code will fall into the Wednesday case:

Weekday valueToCompare = Weekday.Wednesday;

switch (valueToCompare)
{
  case Weekday.Monday:
    Console.WriteLine("I need coffee.");
    break;
  case Weekday.Tuesday:
    Console.WriteLine("I need coffee.");
    break;
  case Weekday.Wednesday:
    Console.WriteLine("I need coffee.");
    break;
  case Weekday.Thursday:
    Console.WriteLine("Thursday is the new Friday!");
    break;
  case Weekday.Friday:
    Console.WriteLine("Thank God it's Friday!");
    break;
  case Weekday.Saturday:
    Console.WriteLine("My favorite day!");
    break;
  case Weekday.Sunday:
    Console.WriteLine("My other favorite day!");
    break;
  default:
    Console.WriteLine("Invalid day of the week.");
    break;
}

How to Iterate Over Enums in C#

You can iterate over enums in C# using the Enum.GetValues() method, which returns an array of all the values in the enum. This code snippet prints each day of the week, including Monday, Tuesday, and so on:

foreach (Weekday day in Enum.GetValues(typeof(Weekday)))
{
  Console.WriteLine((int)day); // To print the index
  Console.WriteLine(day); // To print the text value
}

You can also iterate over enum values using the Enum.GetNames() method. It returns a list of string values to represent the names for the enum options:

foreach (string name in Enum.GetNames(typeof(Weekday)))
{
  int index = (int)(Weekday)Enum.Parse(typeof(Weekday), name);

  Console.WriteLine(index); // To print the index
  Console.WriteLine(name); // To print the text value
}

Using Enums in C#

You can use enums in your code to make it more readable and less prone to bugs. Enums can also be very useful when paired with other structures, such as dictionaries.

ncG1vNJzZmivp6x7rq3KnqysnZ%2Bbe6S7zGiaZquYlr%2BxecSnrKarXw%3D%3D