Enumerations (enum)
An enumeration type (an enum type) is a distinct value type that is defined by a set of named constants of the underlying integral numeric type. An enum type provides a convenient way to define a set of named values, which makes the code more readable and maintainable. For example, you can define an enum type for the days of the week, the colors of the rainbow, or the commands in a menu.
On this page
Defining an enum
You define an enum type by using the enum keyword. The following example defines an enum type named DaysOfWeek:
public enum DaysOfWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
In this example, the enum members are Sunday, Monday, and so on. By default, the underlying type of an enum is int, and the first enum member is assigned the value 0, the second 1, and so on.
You can explicitly assign values to enum members:
public enum ErrorCode : short
{
None = 0,
FileNotFound = 100,
AccessDenied = 101,
NetworkError = 200
}
Underlying type
The underlying type of an enum must be one of the integral numeric types: byte, sbyte, short, ushort, int, uint, long, or ulong. You can specify the underlying type by appending a colon and the type name after the enum name:
public enum StatusCode : byte
{
Ok = 200,
NotFound = 404,
InternalServerError = 500
}
If you don't specify the underlying type, it defaults to int.
Accessing enum members
You can access enum members by using their name. For example, to get the value of DaysOfWeek.Monday, you can do the following:
DaysOfWeek today = DaysOfWeek.Monday;
int dayValue = (int)today; // dayValue will be 1
You can also use the Enum.Parse method to convert a string to an enum member:
string dayName = "Wednesday";
DaysOfWeek day = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), dayName); // day will be DaysOfWeek.Wednesday
Example Usage
Consider a simple program that uses the DaysOfWeek enum:
using System;
public enum DaysOfWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
public class EnumDemo
{
public static void Main(string[] args)
{
DaysOfWeek today = DaysOfWeek.Wednesday;
if (today == DaysOfWeek.Wednesday)
{
Console.WriteLine("Today is Wednesday!");
}
Console.WriteLine($"The value of {today} is {(int)today}");
string dayString = "Friday";
DaysOfWeek futureDay = (DaysOfWeek)Enum.Parse(typeof(DaysOfWeek), dayString);
Console.WriteLine($"Parsed day: {futureDay}");
}
}
This program will output:
Today is Wednesday!
The value of Wednesday is 3
Parsed day: Friday
Enum conversions
You can convert enum values to their underlying integral type and vice versa. However, you cannot implicitly convert between different enum types, even if they have the same underlying type.
- Enum to Integral Type: Use a cast.
- Integral Type to Enum: Use a cast. Note that the integral value must be a valid value for the enum, otherwise a runtime exception might occur if it's not.
Attributes
You can apply attributes to enum types and enum members to provide additional metadata. For example, the [Flags] attribute can be used to indicate that an enum can be treated as a bit field, meaning its members can be combined using the bitwise OR operator.
[Flags]
public enum FilePermissions
{
None = 0,
Read = 1 << 0, // 1
Write = 1 << 1, // 2
Execute = 1 << 2 // 4
}
// Usage:
FilePermissions userPermissions = FilePermissions.Read | FilePermissions.Write;
if ((userPermissions & FilePermissions.Read) == FilePermissions.Read)
{
Console.WriteLine("User has read permission.");
}