Read the syntax
enum defines named constants over an integral underlying type. Comparisons and switch arms can use the names, making domain states easier to read than bare numbers.
Cast an enum to its underlying numeric representation when you need that number. Parse converts text to an enum but can throw for invalid text; TryParse is safer for untrusted input.
Syntax
enum EnumName {\n Constant1, Constant2\n}Examples
Basic Enum
Defining and using a simple set of named constants.
Day today = Day.Wednesday;
if (today == Day.Saturday || today == Day.Sunday)
{
Console.WriteLine("Weekend!");
}
else
{
Console.WriteLine("Weekday");
}
public enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }- Defining and using a simple set of named constants.
- enum defines named constants over an integral underlying type. Comparisons and switch arms can use the names, making domain states easier to read than bare numbers.
- today is Wednesday, so neither weekend comparison matches and the else branch prints Weekday.
Enum with switch
Enums integrate naturally with switch statements and expressions.
string GetAction(TrafficLight light) => light switch
{
TrafficLight.Red => "Stop",
TrafficLight.Yellow => "Slow down",
TrafficLight.Green => "Go",
_ => "Unknown"
};
Console.WriteLine(GetAction(TrafficLight.Green)); // Go
public enum TrafficLight { Red, Yellow, Green }- Enums integrate naturally with switch statements and expressions.
- enum defines named constants over an integral underlying type. Comparisons and switch arms can use the names, making domain states easier to read than bare numbers.
- The switch expression maps TrafficLight.Green to Go. The fallback arm covers values outside the listed names.
Custom Underlying Values
Enum constants map to integers by default (starting at 0), but you can assign your own values explicitly.
HttpStatus status = HttpStatus.NotFound;
Console.WriteLine((int)status); // 404
Console.WriteLine(status); // NotFound
public enum HttpStatus
{
Ok = 200,
NotFound = 404,
ServerError = 500
}- Enum constants map to integers by default (starting at 0), but you can assign your own values explicitly.
- enum defines named constants over an integral underlying type. Comparisons and switch arms can use the names, making domain states easier to read than bare numbers.
- NotFound has an explicit underlying value of 404. Casting prints 404, while printing the enum itself displays NotFound.
Parsing and Iterating Enums
Converting strings to enum values, and looping over every defined constant.
Size chosen = Enum.Parse<Size>("Medium");
Console.WriteLine(chosen); // Medium
foreach (Size s in Enum.GetValues<Size>())
{
Console.WriteLine($"{s} = {(int)s}");
}
public enum Size { Small, Medium, Large }- Converting strings to enum values, and looping over every defined constant.
- enum defines named constants over an integral underlying type. Comparisons and switch arms can use the names, making domain states easier to read than bare numbers.
- Parse turns Medium into a Size value. GetValues then visits each declared Size name and prints its associated integer.
Best practices
- Use enums instead of plain int or string constants whenever a variable should only take one of a fixed, known set of values - it gives compile-time safety
- Take advantage of switch expressions with enums for exhaustive, readable branching on the possible values
- Assign explicit underlying values (like HTTP status codes) when the numeric value has external meaning that must stay stable
- Use PascalCase for enum names and their members, following standard C# naming conventions (not ALL_CAPS as in some other languages)
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference