C# enum also called enumeration data type is a value data type. An enum is a way to define a set of named integral constants that may be assigned to a variable. Each item of an enum is represented by an integral value starting with 0.
 
CSharp Enum 
 
The enum keyword in C# and .NET is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. Usually, an enum is declared as public within a namespace and is available to all classes in the namespace. 
 
An enum in C# is declared by the enum keyword followed by the enumeration name and its values within a bracket.
 
The following is an example of an enum, Day. 
  1. enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
In the above example, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. Here the value of Sunday is 0, Monday is 1, Tues is 2 and so forth.
 
This tutorial and code examples demonstrate the fundamentals of enums, their use cases, and the best practices of enumerations in C# and .NET Core. The following list is an example of some common enums examples.
 

WeekDay

Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

WeekEnd

Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

Rectangle

Left, Top, Bottom, Right

Point

X, Y

Color

Yellow, Orange, Red, Green, Blue, Purple, Black

 
All enums are of type System.Enum. Enum types are limited to long, int, short and byte only. By default, the value of an enum member is int and starts with 0 in an increment of 1.
 

1. How to declare enums?

 
The following code example declares three enums, Day, Month, and Color using C# and .NET.
  1. // Day enum represents number of days in a week  
  2. enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
  3. // Month enum represents months in a year  
  4. enum Months : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };  
  5. // Colors   
  6. enum Colors { Yellow, Orage, Red, Green, Blue, Purple, Black };  
The default numeric value of an enum element starts with 0 and increments by 1. But you can override the numeric value by specifying in the declaration. 
  1. public enum RGB { Red = 2, Green = 4, Blue = 6 };  
Once a value of enum is changed, the next value will automatically be incremental value unless its overridden explicitly.
 

2. How do I enumerate an enum in C#? Or How to loop through all enum values in C#?

 
The GetValues method of System.Enum can be used to loop through an enum values. The following code snippet loops through the Colors enum. 
  1. Console.WriteLine("Read values of the Color enum");  
  2. foreach (int i in Enum.GetValues(typeof(Colors)))  
  3.     Console.WriteLine(i);  
The GetNames method of System.Enum returns a string value of each elements of an enum.
 
The following code snippet loops through all items of an enum. 
  1. Console.WriteLine("Read names of the Color enum");  
  2. foreach (string str in Enum.GetNames(typeof(Colors)))  
  3.     Console.WriteLine(str);  

3. How do convert an enum to a string?

 
ToString() method is used to convert an enum value to a string. The following code example converts three enum values to strings.
  1. //Convert enum to string  
  2. Console.WriteLine(Days.Friday.ToString());  
  3. Console.WriteLine(Months.Mar.ToString());  
  4. Console.WriteLine(Colors.Black.ToString());  

4. How to get enum values in C#?

 
The GetValues method of System.Enum can be used to loop through an enum values. The following code snippet loops through the Colors enum. 
  1. Console.WriteLine("Read values of the Color enum");  
  2. foreach (int i in Enum.GetValues(typeof(Colors)))  
  3.     Console.WriteLine(i);  

5. Can you call an enum by the number value?

 
Enum.GetName can be used to get the name of an enum value. The following code snippet gets the name of the 3rd element of Days enum. 
  1. // Get name of value 3 in day enum  
  2. string s = Enum.GetName(typeof(Days), 3);  
  3. Console.WriteLine(s);   

6. How to get an array of all enum values in C#?

 
The following code example converts an enum values into an array.
  1. enum Months : byte { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };  
  2. string[] months = Enum.GetNames(typeof(Months));  
  3. foreach (string month in months)  
  4.     Console.WriteLine(month);  

7. How to add enum values to a List in C#?

 
The following code snippet converts an enum values into a List object. 
  1. enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
  2. List<string> daysList = new List<string>(Enum.GetNames(typeof(Days)));  
  3. foreach (string day in daysList)  
  4.     Console.WriteLine(day);