How to enumerate all Enum values in C#: a complete guide for beginners

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

Why is the question "How can I enumerate an enum?" so popular?

\n\n

If 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.

\n\n

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\n

What is an enum in C#? A quick reminder

\n\n

Enum (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\n
public enum DayOfWeek\n{\n    Monday,\n    Tuesday,\n    Wednesday,\n    Thursday,\n    Friday,\n    Saturday,\n    Sunday\n}
\n\n

Each 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\n

How to enumerate all enum values: the basic way

\n\n

The 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:

\n\n
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\n

Execution result:

\n
New\nInProgress\nCompleted\nCancelled
\n\n

Note: we explicitly cast the result to an array Status[] because Enum.GetValues() returns an Array (untyped).

\n\n

Alternative: Enum.GetNames()

\n\n

If you need not the values themselves, but only their string names, use Enum.GetNames():

\n\n
string[] statusNames = Enum.GetNames(typeof(Status));\nforeach (string name in statusNames)\n{\n    Console.WriteLine(name);\n}
\n\n

The result will be the same, but the element type is string, not Status. This is convenient when working with UI or serialization.

\n\n

Modern approach: generic version (C# 7.3+)

\n\n

Starting 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:

\n\n
Status[] allStatuses = Enum.GetValues<Status>();\n\nforeach (Status status in allStatuses)\n{\n    Console.WriteLine($"ID: {(int)status}, Name: {status}");\n}
\n\n

This method is shorter and type-safe. If you are using a modern version of C# (and you should be!), I recommend this one.

\n\n

Real-life example: populating a dropdown list

\n\n

Imagine 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\n
using 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                

Blogs

Book Recommendations