How To Use Enum In C#

Introduction 

Enum (or Enumeration ) is a value data type in C#. It is mainly used to assign the names or string values to integral constants, that make a program easy to read and maintain. For example, include natural enumerated types (like the planets, days of the week, colors, directions, etc.). The main objective of Enum is to define our own data types(Enumerated Data Types). Enumeration is declared using Enum keyword directly inside a namespace, class, or structure.

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week.

Syntax:

enum  Enum_variable
{
string_1,
string_2,
string_3,
.
.
}

How to declare enum?

enum Colors 
{
    Yellow,
    Orange,
    Red,
    Green,
    Blue,
    Purple,
    Black 
}

Above, the Colors enum declares members in each line separated by a comma.

Enum Values

If values are not assigned to enum members, then the compiler will assign integer values to each member starting with zero by default. The first member of an enum will be 0, and the value of each successive enum member is increased by 1.

enum Colors
{
    Yellow,    // 0
    Orange,    // 1
    Red,       // 2
    Green,     // 3
    Blue,      // 4
    Purple,    // 5
    Black      // 6
}

You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

enum Colors
{
    Yellow = 0,
    Orange = 1,
    Red = 2,
    Green = 3,
    Blue = 4,
    Purple = 5,
    Black = 6
}

You can even assign values to each member.

You can have the enumerator structure start at any integer.

enum Colors 
{
    Yellow = 50, 
    Orange,
    Red,
    Green, 
    Blue, 
    Purple,
    Black 
}

public static void Main()
{
      System.Console.WriteLine((int)Colors.Yellow);
      System.Console.WriteLine((int)Colors.Orange);     
      System.Console.WriteLine((int)Colors.Red);     
      System.Console.WriteLine((int)Colors.Green);
      System.Console.WriteLine((int)Colors.Blue);
      System.Console.WriteLine((int)Colors.Purple);     
      System.Console.WriteLine((int)Colors.Black);     
}

The output for this program is the following.
50
51
52
53
54
55
56

You can also change the values in the center or even the end of the enum structure.

enum Colors 
{
    Yellow, 
    Orange,
    Red,
    Green = 10, 
    Blue, 
    Purple,
    Black 
}
 
public static void Main()
{
      System.Console.WriteLine((int)Colors.Yellow);
      System.Console.WriteLine((int)Colors.Orange);     
      System.Console.WriteLine((int)Colors.Red);     
      System.Console.WriteLine((int)Colors.Green);
      System.Console.WriteLine((int)Colors.Blue);
      System.Console.WriteLine((int)Colors.Purple);     
      System.Console.WriteLine((int)Colors.Black);     
}

The output for this program is the following.
0
1
2
10
11
12
13

You can assign multiple variables the same value or give them a custom value that overrides the default.

enum Colors 
{
    Yellow = 10, 
    Orange = 12,
    Red = 14,
    Green = 16, 
    Blue = 18, 
    Purple = 20,
    Black = 22 
}

public static void Main()
{
      System.Console.WriteLine((int)Colors.Yellow);
      System.Console.WriteLine((int)Colors.Orange);     
      System.Console.WriteLine((int)Colors.Red);     
      System.Console.WriteLine((int)Colors.Green);
      System.Console.WriteLine((int)Colors.Blue);
      System.Console.WriteLine((int)Colors.Purple);     
      System.Console.WriteLine((int)Colors.Black);     
}

The output for this program is the following.
10
12
14
16
18
20
22

Access an Enum

An enum can be accessed using the dot syntax: enum.member

enum Colors 
{
    Yellow, 
    Orange,
    Red,
    Green, 
    Blue, 
    Purple,
    Black 
}

Console.WriteLine(Colors.Yellow);   // Yellow
Console.WriteLine(Colors.Orange);   // Orange
Console.WriteLine(Colors.Red);      // Red
Console.WriteLine(Colors.Green);    // Green
Console.WriteLine(Colors.Blue);     // Blue
Console.WriteLine(Colors.Purple);   // Purple
Console.WriteLine(Colors.Black);    // Black

Conversion

enum Colors 
{
    Yellow = 0,
    Orange = 1,
    Red = 2,
    Green = 3,
    Blue = 4,
    Purple = 5,
    Black = 6
}

Console.WriteLine(Colors.Red); //output: Red
int value = (int)Colors.Red; // enum to int conversion
Console.WriteLine(value); // output: 2 
    
var color = (Colors)5; // int to enum conversion
Console.WriteLine(color); // output: Purple

How to loop through all enum values 

The GetValues method of System.Enum can be used to loop through an enum values. The following code snippet loops through the Colors enum.

Console.WriteLine("Read values of the Color enum");  
foreach (int i in Enum.GetValues(typeof(Colors)))  
    Console.WriteLine(i);

The GetNames method of System.Enum returns a string value of each elements of an enum.

Console.WriteLine("Read names of the Color enum");  
foreach (string str in Enum.GetNames(typeof(Colors)))  
    Console.WriteLine(str);

Convert an enum to a string

ToString() method is used to convert an enum value to a string.

Console.WriteLine(Colors.Yellow.ToString()); 
Console.WriteLine(Colors.Red.ToString()); 
Console.WriteLine(Colors.Blue.ToString());

Get enum values 

Console.WriteLine("Read values of the Color enum");  
foreach (int i in Enum.GetValues(typeof(Colors)))  
    Console.WriteLine(i);

Get an array of all enum values

enum Colors 
{
    Yellow, 
    Orange,
    Red,
    Green, 
    Blue, 
    Purple,
    Black 
}

string[] Colors = Enum.GetNames(typeof(Colors));  
foreach (string color in Colors)  
    Console.WriteLine(color)

Add enum values to a List

enum Colors 
{
    Yellow, 
    Orange,
    Red,
    Green, 
    Blue, 
    Purple,
    Black 
}

List<string> colorsList = new List<string>(Enum.GetNames(typeof(Colors)));  
foreach (string color in colorsList)  
    Console.WriteLine(color);

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories