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.
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:
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.
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.
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.
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.
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.GetNamesandEnum.GetValuesare 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.GetNamesonly 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
- How to loop through all the files in a directory in c .net?
- How to make a .NET Windows Service start right after the installation?
- How to make a window always stay on top in .Net?
- How to make Entity Framework Data Context Readonly
- How to make IEnumerablestring.Contains case-insensitive?
- How to make inline functions in C
- How to make make a .NET COM object apartment-threaded?
- How to make modal dialog in WPF?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.