How to Iterate Through All Enum Values in C#: From Beginner to Pro
\n\nImagine 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.
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\nLet's go!
\n\n\n\n
What is an Enum in C# and Why Iterate Through It?
\n\nAn 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.
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
Instead of writing:
\nstring[] 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\nThis 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.
Code example:
\nusing 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\nConsole output:
\nAll difficulty levels:\nLevel: Easy (numeric value: 0)\nLevel: Normal (numeric value: 1)\nLevel: Hard (numeric value: 2)\nLevel: Insane (numeric value: 3)\n\nImportant: 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.
// .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\nSometimes 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.
Example:
\nstring[] names = Enum.GetNames(typeof(Difficulty));\nConsole.WriteLine("Level names:");\nforeach (string name in names)\n{\n Console.WriteLine(name);\n}\n\nOutput:
\nLevel names:\nEasy\nNormal\nHard\nInsane\n\nWhen to use? If you need strings specifically (e.g., for display in the interface or logging), rather than the enum constants themselves.