C#
enums
looping
programming
how-to

How to loop through all enum values in C?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C#, looping through all enum values is straightforward with Enum.GetValues. The main things to understand are the difference between values and names, the modern generic form available in newer .NET versions, and the special caution required for [Flags] enums where combinations do not behave like simple standalone values.

Core Sections

The standard approach: Enum.GetValues

For a normal enum, the basic pattern is:

csharp
1using System;
2
3public enum DayOfWeekShort
4{
5    Sunday,
6    Monday,
7    Tuesday,
8    Wednesday,
9    Thursday,
10    Friday,
11    Saturday
12}
13
14public class Program
15{
16    public static void Main()
17    {
18        foreach (DayOfWeekShort day in Enum.GetValues(typeof(DayOfWeekShort)))
19        {
20            Console.WriteLine(day);
21        }
22    }
23}

This is the classic and widely supported answer. It gives you the actual enum values, not just their names.

Prefer the generic form when available

In newer .NET versions, the generic form is cleaner because it avoids casting.

csharp
1using System;
2
3public enum Color
4{
5    Red,
6    Green,
7    Blue
8}
9
10public class Program
11{
12    public static void Main()
13    {
14        foreach (var color in Enum.GetValues<Color>())
15        {
16            Console.WriteLine(color);
17        }
18    }
19}

This is usually the nicest modern syntax when your target framework supports it.

Enum.GetNames is different

If you only want the text names, not the enum values themselves, use Enum.GetNames.

csharp
1using System;
2
3public enum Status
4{
5    Pending,
6    Running,
7    Finished
8}
9
10public class Program
11{
12    public static void Main()
13    {
14        foreach (string name in Enum.GetNames(typeof(Status)))
15        {
16            Console.WriteLine(name);
17        }
18    }
19}

This matters when you want labels for UI, diagnostics, or dropdowns rather than values for branching logic.

Sorting and underlying values

Enums often map to integers, and Enum.GetValues returns them in the order of their underlying numeric values, not necessarily the textual order you wrote if you have custom assignments.

csharp
1using System;
2
3public enum Priority
4{
5    Low = 10,
6    Medium = 20,
7    High = 30
8}
9
10public class Program
11{
12    public static void Main()
13    {
14        foreach (var priority in Enum.GetValues<Priority>())
15        {
16            Console.WriteLine($"{priority} = {(int)priority}");
17        }
18    }
19}

That is usually fine, but it is worth knowing if the numeric values are sparse or intentionally ordered.

[Flags] enums need extra care

A [Flags] enum represents bitwise combinations, so iterating all declared values is not the same as iterating all possible combinations.

csharp
1using System;
2
3[Flags]
4public enum FileAccessMode
5{
6    None = 0,
7    Read = 1,
8    Write = 2,
9    Execute = 4
10}
11
12public class Program
13{
14    public static void Main()
15    {
16        foreach (var mode in Enum.GetValues<FileAccessMode>())
17        {
18            Console.WriteLine(mode);
19        }
20    }
21}

This prints the declared constants only. It does not automatically generate combinations such as Read | Write. That distinction is important when writing validation, permission UIs, or bitmask utilities.

Common real uses

Looping through enum values is often used for:

  • building dropdown lists
  • generating menus
  • validating user input against a known set of states
  • running one test case per enum value

Because these are common patterns, it is usually better to iterate the enum directly than to duplicate its values in a separate list that can drift out of sync.

Common Pitfalls

  • Confusing the title wording with the language C is easy here, but the actual solution uses C# enum APIs such as Enum.GetValues.
  • Assuming Enum.GetNames and Enum.GetValues are interchangeable ignores the difference between strings and typed enum values.
  • Forgetting that the modern generic Enum.GetValues<T>() may not exist on older target frameworks can cause compatibility surprises.
  • Treating [Flags] enums like ordinary enums overlooks the fact that combinations are not automatically enumerated.
  • Duplicating enum options manually in code instead of iterating the enum itself creates maintenance drift when values change.

Summary

  • In C#, loop through enum values with Enum.GetValues.
  • Prefer Enum.GetValues<T>() when your target framework supports the generic form.
  • Use Enum.GetNames only when you need the string labels.
  • Be careful with [Flags] enums because declared values and possible combinations are different concepts.
  • Iterating the enum directly is cleaner and safer than maintaining a separate hardcoded list.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.