NeuroAgent

How to Iterate Through Enum Values in C#

Learn the correct ways to iterate through all enum values in C#. Discover Enum.GetValues, Enum.GetValues<TEnum>, and performance-optimized approaches for .NET 5+ and older versions.

Question

How to iterate through all values of an enum in C#?

csharp
public enum Foos
{
    A,
    B,
    C
}

What is the correct way to loop through all possible values of the Foos enum in C#? I’m looking for a solution similar to:

csharp
foreach(Foo in Foos)

but with the correct syntax.

NeuroAgent

To iterate through all values of an enum in C#, you need to use the Enum.GetValues method or its generic version Enum.GetValues<TEnum> available since .NET 5. The syntax you’re looking for isn’t directly possible with foreach(Foo in Foos), but there are several effective approaches.

Using Enum.GetValues Method

The most straightforward approach is to use Enum.GetValues() with a foreach loop. This method returns an array containing all values of the specified enum type.

csharp
public enum Foos
{
    A,
    B,
    C
}

// Using Enum.GetValues with foreach
foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    Console.WriteLine(foo);
}

// Output:
// A
// B
// C

This approach works with any enum type, including those with custom underlying values or “holes” in their sequence source.

Using Generic Enum.GetValues Method

Since .NET 5, you can use the generic version which provides better type safety and avoids boxing/unboxing operations:

csharp
// .NET 5+ approach - preferred method
var values = Enum.GetValues<Foos>();
foreach (Foos foo in values)
{
    Console.WriteLine(foo);
}

// Or directly in foreach
foreach (Foos foo in Enum.GetValues<Foos>())
{
    Console.WriteLine(foo);
}

The generic version is more efficient because it doesn’t require casting or boxing operations source.

Using Enum.GetNames for String Names

If you only need the string names of enum values without their underlying integer representations, you can use Enum.GetNames():

csharp
// Get string names only
foreach (string name in Enum.GetNames(typeof(Foos)))
{
    Console.WriteLine(name);
}

// Output:
// A
// B
// C

This method returns a string array rather than the actual enum values source.

Performance Considerations

When choosing between methods, consider these performance characteristics:

  1. Enum.GetValues() (NET 5+) - Most efficient, avoids boxing
  2. Enum.GetValues(typeof(T)) - Good performance, requires boxing
  3. Enum.GetNames() - Fastest if you only need strings, but limited to names only

⚠️ Important: Never try to iterate through enum values by incrementing integers, as enums can have “holes” in their values. For example, an enum like enum E { A = 1, B = 2, C = 4 } would miss value 3 if you iterate by incrementing source.

Complete Utility Class Example

For reusable enum iteration functionality, consider this utility class:

csharp
public static class EnumUtil
{
    public static IEnumerable<T> GetValues<T>() where T : Enum
    {
        return Enum.GetValues(typeof(T)).Cast<T>();
    }
    
    // Usage:
    foreach (Foos foo in EnumUtil.GetValues<Foos>())
    {
        Console.WriteLine(foo);
    }
}

This utility class provides a clean, reusable way to iterate through any enum type with proper generics support source.


Conclusion

  1. For .NET 5+ projects, use Enum.GetValues<TEnum>() for the most efficient and type-safe iteration
  2. For older .NET versions, use Enum.GetValues(typeof(T)) with proper casting
  3. When you only need string names, use Enum.GetNames(typeof(T))
  4. Avoid integer incrementation as it won’t handle enums with gaps or custom values properly
  5. Consider creating a utility class if you frequently need to iterate through enums in your codebase

The correct way to loop through all possible values of your Foos enum is either:

csharp
foreach (Foos foo in Enum.GetValues<Foos>()) // .NET 5+

or

csharp
foreach (Foos foo in Enum.GetValues(typeof(Foos))) // All versions

Sources

  1. Stack Overflow: How to loop through all enum values in C#?
  2. Microsoft Learn: Enum.GetValues Method
  3. Tutorial Teacher: How to loop through an enum in C#?
  4. Chris Pietschmann: C#: How to Enumerate over Enum values and names
  5. Net-Informations: How to loop through all enum values in C#
  6. Stack Overflow: Why is Enum.GetValues better than iterating?