In this article, we’re going to look at some of the ways we can enumerate an Enum in C#. Enums allow us to declare a group of related constant values in a readable way where the underlying data type is usually an integer. 

To download the source code for this article, you can visit our GitHub repository.

Let’s start.

Creating an Example Enum

Let’s start by creating an enumeration we can use throughout the rest of this article:

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
internal enum DayOfWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

We’ve made an enumeration called DayOfWeek where each value is one of the days of the week.

How to Enumerate Using Enum.GetValues(Type type)

Before we enumerate our DayOfWeek enum we need to get the values into an enumerable type. Fortunately, there’s a handy static method on the Enum class that allows us to get the values of an enumeration into an Array object:

var daysOfWeek = Enum.GetValues(typeof(DayOfWeek));

The return value of the method Enum.GetValues(Type type) is Array. 

Now we can loop through the values in that array and do something with each value:

foreach (var dayOfWeek in daysOfWeek)
{
    Console.WriteLine(dayOfWeek);
}

The below output will be the result:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

The application prints out the name of the constants. This is because the result of Enum.GetValues(Type type) is an Array object which holds an array of type object. When we Console.WriteLine() with an object type, the ToString() method is what’s used.

To get the integer value of each enumeration value we can cast the value of dayofWeek to an int:

Console.WriteLine((int)dayOfWeek);

Alternatively, we can specify that we want dayOfWeek to be an int when we declare it in our loop:

foreach (int dayOfWeek in daysOfWeekValues)
{
    Console.WriteLine(dayOfWeek);
}

This will produce an output in the console:

0
1
2
3
4
5
6

How to Enumerate Using Enum.GetValues<TEnum>()

Since .NET 5, there has been another version of the Enum.GetValues method and this one is a generic method.

Instead of calling Enum.GetValues(Type type) and passing in the type of our enumeration using the typeof keyword, we can pass our DayOfWeek enumeration as a type parameter:

var daysOfWeek = Enum.GetValues<DayOfWeek>();

By calling Enum.GetValues this way, the return type is an IEnumerable<DayOfWeek> so we don’t need to do any casting. Our daysOfWeek variable is now implicitly of type IEnumerable<DayOfWeek>.

We can loop through this in the same way that we have before:

foreach (var dayOfWeek in Enum.GetValues<DayOfWeek>())
{
    Console.WriteLine($"{dayOfWeek} = {(int)dayOfWeek}");
}

And, let’s inspect the output:

Monday = 0
Tuesday = 1
Wednesday = 2
Thursday = 3
Friday = 4
Saturday = 5
Sunday = 6

How to Enumerate an Enum Using Reflection

Using reflection in C# should always be done with a lot of care because it can be very CPU-intensive and not something that you would typically want to do a lot of. In any case, we can use reflection to enumerate our DayOfWeek enumeration.

First, we want to get the field information for our enumeration:

FieldInfo[] fields = typeof(DayOfWeek).GetFields(BindingFlags.Static | BindingFlags.Public);

This will give us a FieldInfo for each of the enumeration values. Now we’ll use LINQ to:

  • Select the value of GetValue(null)
  • Cast each object result to our enumeration type
  • Return the results as an array
public static TEnum[] GetValuesWithReflection<TEnum>() where TEnum : Enum
{
    FieldInfo[] fields = typeof(TEnum).GetFields(BindingFlags.Static | BindingFlags.Public);

    return fields.Select(x => x.GetValue(null))
        .Cast<TEnum>()
        .ToArray();
}

Reflection With GetEnumValues() or GetEnumNames() Methods

We can also use the inbuilt methods Type.GetEnumValues() or Type.GetEnumNames() to get the same result:

Type dayOfWeekEnum = typeof(DayOfWeek);
var values = dayOfWeekEnum.GetEnumValues();
foreach (var value in values)
{
    Console.WriteLine(value);
}

var names = dayOfWeekEnum.GetEnumNames();
foreach (var name in names)
{
    Console.WriteLine(name);
}

This results in the same exact output.

How Should We Enumerate Enums in C#?

The best way to enumerate over the values of an enumeration if you are using .NET 5 or higher would be to use the generic Enum.GetValues<T>() method. This is a really handy method because it already returns the result as an IEnumerable collection of your enumeration’s type.

If you are using an earlier version of .NET that doesn’t support the generic Enum.GetValues<T>() method, the best option would be to use the Enum.GetValues(Type type) method to get the enumeration values as an Array object. Then we can cast it to an array of enumeration types.

We recommend against using reflection to enumerate an enum unless the other two options can’t be used. If reflection has to be used to enumerate an enum then we would strongly recommend using a caching strategy so the conversion doesn’t have to be done over and over again as it can be very expensive. 

Conclusion

In this article, we’ve learned how to enumerate an enum in C#. We’ve learned how to get an enum into an enumerable type in .NET versions older than .NET 5 and newer so that we can do what we want with each possible value of the enum.

Finally, we’ve shown how we can use reflection to enumerate an enumeration. However, we don’t recommend using reflection more than we require it.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!