How to iterate through all values of an enum in C#?
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:
foreach(Foo in Foos)
but with the correct syntax.
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
- Using Generic Enum.GetValues
Method - Using Enum.GetNames for String Names
- Performance Considerations
- Complete Utility Class Example
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.
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:
// .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():
// 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:
- Enum.GetValues
() (NET 5+) - Most efficient, avoids boxing - Enum.GetValues(typeof(T)) - Good performance, requires boxing
- 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:
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
- For .NET 5+ projects, use
Enum.GetValues<TEnum>()for the most efficient and type-safe iteration - For older .NET versions, use
Enum.GetValues(typeof(T))with proper casting - When you only need string names, use
Enum.GetNames(typeof(T)) - Avoid integer incrementation as it won’t handle enums with gaps or custom values properly
- 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:
foreach (Foos foo in Enum.GetValues<Foos>()) // .NET 5+
or
foreach (Foos foo in Enum.GetValues(typeof(Foos))) // All versions
Sources
- Stack Overflow: How to loop through all enum values in C#?
- Microsoft Learn: Enum.GetValues Method
- Tutorial Teacher: How to loop through an enum in C#?
- Chris Pietschmann: C#: How to Enumerate over Enum values and names
- Net-Informations: How to loop through all enum values in C#
- Stack Overflow: Why is Enum.GetValues better than iterating?