Top 7 C# Enum Code Examples

In C#, an enum (short for "enumeration") is a value type that consists of a set of named constants. An enum can be defined using the enum keyword, followed by the name of the enum and a list of comma-separated identifiers representing the named constants. This article and code example teach you how to use enums in C# and .NET.

A C# enum data type represents multiple values. Each item of an enum is represented by an integral value starting with 0.

Introduction

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. 

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. So here, the value of Sunday is 0, Monday is 1, Tues is two, and so forth.

The following list is an example of some common enums examples.

WeekDay Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday
Month 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. By default, the value of an enum member is int and starts with 0 in an increment of 1.

1. How to declare an enum in C#?

The following code example declares three enums, Day, Month, and Color, using C# and .NET.

// Day enum represents number of days in a week  
enum Days {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
};
// Month enum represents months in a year  
enum Months: byte {
    Jan,
    Feb,
    Mar,
    Apr,
    May,
    Jun,
    Jul,
    Aug,
    Sep,
    Oct,
    Nov,
    Dec
};
// Colors   
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 it in the declaration. 

public enum RGB {
    Red = 2, Green = 4, Blue = 6
};

Once the value of the enum is changed, the next value will automatically be incremental unless explicitly overridden.

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. 

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.

The following code snippet loops through all items of an enum. 

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

3. How do you convert an enum to a string?

ToString() method converts an enum value to a string. The following code example converts three enum values to strings.

//Convert enum to string  
Console.WriteLine(Days.Friday.ToString());
Console.WriteLine(Months.Mar.ToString());
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. 

Console.WriteLine("Read values of the Color enum");  
foreach (int i in Enum.GetValues(typeof(Colors)))  
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 the Days enum. 

// Get name of value 3 in day enum  
string s = Enum.GetName(typeof(Days), 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.

enum Months: byte {
    Jan,
    Feb,
    Mar,
    Apr,
    May,
    Jun,
    Jul,
    Aug,
    Sep,
    Oct,
    Nov,
    Dec
};
string[] months = Enum.GetNames(typeof(Months));
foreach(string month in months)
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. 

enum Days {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
};
List < string > daysList = new List < string > (Enum.GetNames(typeof(Days)));
foreach(string day in daysList)
Console.WriteLine(day);


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.