How to Iterate Through All Enum Values in C#: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Iterate Through All Enum Values in C#: From Beginner to Pro

\n\n

Imagine this scenario: you're writing a game in C#, and you have an enumeration Difficulty with difficulty levels: Easy, Normal, Hard, Insane. You need to display them all in a selection menu, or, say, create a dropdown list for a configurator. How do you do it? Manually write an array of strings? And what if a new level is added tomorrow? You'll have to dig into the code and change it in three places.

\n\n

Fortunately, C# has an elegant solution. The question "How to loop through all enum values in C#?" is one of the most popular on Stack Overflow (hundreds of thousands of views). And today we'll cover it completely. You'll learn not just one, but 4 ways to iterate through enum values, understand which one to use when, and get ready-to-use code you can copy into your project.

\n\n

Let's go!

\n\n
\n\n

What is an Enum in C# and Why Iterate Through It?

\n\n

An Enum (enumeration) is a set of named constants. Instead of remembering that 0 = Easy, 1 = Normal, 2 = Hard, you simply write Difficulty.Easy. The code becomes readable and safe.

\n\n

But when a project has 10-20 enum values, sooner or later you'll face the task: get them all. For example:

\n
    \n
  • Populate a ComboBox or Dropdown in the UI.
  • \n
  • Create test data for all variants.
  • \n
  • Apply attributes to each value.
  • \n
  • Compare two enum sets.
  • \n
\n\n

Instead of writing:

\n
string[] names = { "Easy", "Normal", "Hard", "Insane" };
\n\n

…we use reflection and built-in .NET methods. It's flexible, scalable, and doesn't require manual updates.

\n\n
\n\n

Method 1: The Classic — Enum.GetValues (Most Popular)

\n\n

This is the answer you'll find on Stack Overflow with the most upvotes. The Enum.GetValues(typeof(YourEnum)) method returns an array of all values of a specific enumeration.

\n\n

Code example:

\n
using System;\n\npublic enum Difficulty\n{\n    Easy,\n    Normal,\n    Hard,\n    Insane\n}\n\nclass Program\n{\n    static void Main()\n    {\n        Console.WriteLine("All difficulty levels:");\n        \n        // Get the array of enum values\n        Difficulty[] allDifficulties = (Difficulty[])Enum.GetValues(typeof(Difficulty));\n        \n        foreach (Difficulty level in allDifficulties)\n        {\n            Console.WriteLine($"Level: {level} (numeric value: {(int)level})");\n        }\n    }\n}
\n\n

Console output:

\n
All difficulty levels:\nLevel: Easy (numeric value: 0)\nLevel: Normal (numeric value: 1)\nLevel: Hard (numeric value: 2)\nLevel: Insane (numeric value: 3)
\n\n

Important: Enum.GetValues returns an array of type Array, so you need to explicitly cast it to your enum type. Starting with .NET 5, a generic version appeared: Enum.GetValues<Difficulty>(), which is slightly more convenient.

\n\n
// .NET 5+ \nDifficulty[] allDifficulties = Enum.GetValues<Difficulty>();\nforeach (var level in allDifficulties)\n{\n    Console.WriteLine(level);\n}
\n\n
\n\n

Method 2: Get Names via Enum.GetNames

\n\n

Sometimes you need not the values themselves, but their string names (e.g., for the UI). For this, there's Enum.GetNames. It returns an array of strings.

\n\n

Example:

\n
string[] names = Enum.GetNames(typeof(Difficulty));\nConsole.WriteLine("Level names:");\nforeach (string name in names)\n{\n    Console.WriteLine(name);\n}
\n\n

Output:

\n
Level names:\nEasy\nNormal\nHard\nInsane
\n\n

When to use? If you need strings specifically (e.g., for display in the interface or logging), rather than the enum constants themselves.

Blogs

Book Recommendations