C# Enum.GetValues: Declare, Iterate, Flags, and Convert to String
An enum (enumeration) is a named set of integer constants. Enums make code more readable than magic numbers and give the compiler the ability to catch invalid values at compile time.
By default the underlying type is int and values start at 0, but both can be changed.
Quick answer: use
Enum.GetValues<T>()to iterate through every value in an enum, and useEnum.GetName()or.ToString()when you need the corresponding name.
On this page
Declaring an Enum
Using an Enum
Iterate All Values with Enum.GetValues
Enum.GetNames: All Names as Strings
Convert to String and Parse
Enum.IsDefined: Validate a Value
Flags Enum: Bitwise Combinations
Apply [Flags] when an enum value can be a combination of members. Use powers of 2 for the values.
Enum as a Method Parameter
Real-World Example: Order State Machine
Quick Reference
| Operation | Code |
|---|---|
| Get all values | Enum.GetValues<T>() |
| Get all names | Enum.GetNames<T>() |
| Parse string safely | Enum.TryParse<T>(str, out var val) |
| Convert to int | (int)myEnum |
| Convert int to enum | (MyEnum)intValue |
| Validate an integer | Enum.IsDefined<T>((T)value) |
| Enum to string | myEnum.ToString() |
| Test Flags bit | myEnum.HasFlag(flag) |
| Add a flag | myEnum |= flag |
| Remove a flag | myEnum &= ~flag |
Take It Further

The community's favorite C# guide. Teaches every concept you just read about with clarity.
The C# Player's Guide: 5th Edition · RB Whitaker
Get it on Amazon →As an Amazon Associate I earn from qualifying purchases.
Related Articles
Cast an int to an enum type in C# for a direct conversion. Learn how to safely convert integers to enum values using Enum.IsDefined and Enum.TryParse.
Switch expressions in C# 8.0+ provide concise pattern matching that returns a value directly. They replace verbose switch statements and eliminate break.
Learn C# pattern matching with is expressions, switch expressions, type patterns, property patterns, relational patterns, and list patterns.
const fields are compile-time constants set at declaration. readonly fields are runtime constants that can be assigned in the constructor in C#.