Enumerate enum names, values and flags in C#
An enum has named constants, underlying numeric values and potentially several names for the same value. Decide which of those your loop needs. A settings menu may need every declared name. A calculation may need each distinct value. A permissions editor usually needs individual flag bits rather than every named constant.
Use Enum.GetNames<TEnum>() for names and Enum.GetValues<TEnum>() for values. Neither operation discovers every integer that can be cast to the enum. Microsoft documents that values include duplicates when members are aliases, and that converting those values back to names does not recover every original name.
Run the example
Install the .NET 10 SDK, create a console project with dotnet new console -n EnumNames -f net10.0, and replace Program.cs with this program. Run it with dotnet run --project EnumNames from the parent directory.
using System;
using System.Linq;
foreach (var name in Enum.GetNames<Phase>().Order(StringComparer.Ordinal)) {
var value = Enum.Parse<Phase>(name);
Console.WriteLine($"{name}={(int)value}");
}
var values = Enum.GetValues<Phase>();
Console.WriteLine($"Named members={values.Length}");
Console.WriteLine($"Distinct values={values.Distinct().Count()}");
Console.WriteLine($"Named permissions={Enum.GetValues<Permission>().Length}");
Console.WriteLine($"Read and Write={(int)(Permission.Read | Permission.Write)}");
enum Phase {
Queued = 10,
Running = 20,
Active = 20,
Finished = 30
}
[Flags]
enum Permission {
None = 0,
Read = 1,
Write = 2,
Execute = 4,
All = Read | Write | Execute
}
The expected output is:
Active=20
Finished=30
Queued=10
Running=20
Named members=4
Distinct values=3
Named permissions=5
Read and Write=3
The program orders names explicitly, then parses each known name to obtain its numeric value. Parsing is safe here because the names came from that same enum. Accepting user input requires a separate validation policy.
Preserve aliases and choose display order
Active and Running are distinct names for the value 20. If you enumerate values and call ToString(), you cannot treat the chosen alias as a stable display label. Keep the names when the distinction matters. If aliases are interchangeable, Distinct() over the values removes the duplicate numeric value.
The enum APIs do not promise source declaration order as a presentation contract. Values are ordered by their unsigned numeric magnitude. Negative members can therefore appear after positive ones. This example uses an ordinal name sort to make its output stable. A product menu that needs a business order should maintain that order explicitly, for example with a list of allowed values and display labels.
Renaming a member is also different from changing a translated caption. Persist a deliberate wire value or stable numeric code rather than a localized label. A name-based configuration format must account for renamed or retired members.
Treat flags as a bit set
The flags enum declares five names, but its three independent bits allow eight combinations. Read | Write has the value 3 even though no member has that name. GetValues returns declared constants, including None and the composite All. It does not generate the powerset of combinations.
For a permission checklist, explicitly select the individual permissions you want users to toggle. Automatically treating every named constant as an independent checkbox would add both All and its constituent bits. This creates redundant controls and an unclear update policy.
Enumeration is consequently a discovery operation over declarations. It does not establish that an arbitrary number is allowed by your application, nor that a flags combination contains only supported bits. Keep those input checks at the boundary where values enter the system.
References



Question provenance
This article addresses How can I enumerate an enum? , asked by Ian Boyd on Stack Overflow. The source question is licensed under CC BY-SA 4.0 . The explanation and examples here are newly authored for current .NET. No answer text or code is reproduced.
Author: Herman Schoenfeld