Why is the question "How can I enumerate an enum?" so popular?
\n\nIf you've ever written in C# and encountered enumerations (enum), you've probably wondered: "How do I get all the values of this enum?". This question is a real hit on Stack Overflow, with thousands of views. And it's no surprise: enumerating an enum is one of those tasks that seems simple, but without knowing the correct syntax, it can stump even an experienced developer.
In this article, we'll break down how to properly enumerate all enum values in C#, show working code examples you can copy directly into your project, and discuss potential pitfalls. Let's go!
\n\nWhat is an enum in C#? A quick reminder
\n\nEnum (enumeration) is a data type that allows you to define a set of named constants. For example, days of the week, order statuses, or error types. In C#, an enum is declared like this:
\n\npublic enum DayOfWeek\n{\n Monday,\n Tuesday,\n Wednesday,\n Thursday,\n Friday,\n Saturday,\n Sunday\n}\n\nEach element is assigned an integer value by default (starting from 0). But often there is a need to iterate over all enum elements — for example, to display them in a dropdown list, create an array, or check if a value exists.
\n\nHow to enumerate all enum values: the basic way
\n\nThe most popular and simple way is to use the static method Enum.GetValues(). It returns an array of all values of the specified enumeration. Here's how it works:
using System;\n\npublic class Program\n{\n public enum Status\n {\n New,\n InProgress,\n Completed,\n Cancelled\n }\n\n public static void Main()\n {\n // Get all enum values\n Status[] allStatuses = (Status[])Enum.GetValues(typeof(Status));\n\n // Iterate over them with foreach\n foreach (Status status in allStatuses)\n {\n Console.WriteLine(status);\n }\n }\n}\n\nExecution result:
\nNew\nInProgress\nCompleted\nCancelled\n\nNote: we explicitly cast the result to an array Status[] because Enum.GetValues() returns an Array (untyped).
Alternative: Enum.GetNames()
\n\nIf you need not the values themselves, but only their string names, use Enum.GetNames():
string[] statusNames = Enum.GetNames(typeof(Status));\nforeach (string name in statusNames)\n{\n Console.WriteLine(name);\n}\n\nThe result will be the same, but the element type is string, not Status. This is convenient when working with UI or serialization.
Modern approach: generic version (C# 7.3+)
\n\nStarting with C# 7.3, we have a more elegant way — Enum.GetValues<T>(). It returns an array of the specific type, and no casting is required:
Status[] allStatuses = Enum.GetValues<Status>();\n\nforeach (Status status in allStatuses)\n{\n Console.WriteLine($"ID: {(int)status}, Name: {status}");\n}\n\nThis method is shorter and type-safe. If you are using a modern version of C# (and you should be!), I recommend this one.
\n\nReal-life example: populating a dropdown list
\n\nImagine you are writing a task management application. You need to show the user a list of possible statuses in a dropdown menu. Here's how it's done using enum enumeration:
\n\nusing System;\nusing System.Collections.Generic;\n\npublic class TaskManager\n{\n public enum TaskStatus\n {\n NotStarted,\n InProgress,\n WaitingForReview,\n Done\n }\n\n publ