Enumeration in C#

There are many situations when we want to give specific names to values. In order to accomplish this task we use Enumeration.

In this blog I am going to explain how to create and use enumeration (enum).

Enumerations are special sets of named values which all maps to a set of numbers, usually integers. The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sunday is 0, Monday is 1, Tuesday is 2, and so forth.

Example:

enum Days 
{ 
     Sunday, 
     Monday, 
     Tuesday, 
     Wednesday, 
     Thursday, 
     Friday, 
     Saturday 
};

static void Main()
{
     int x = (int)Days.Sunday;
     int y = (int)Days.Friday;
     Console.WriteLine("Sunday = {0}", x);
     Console.WriteLine("Friday = {0}", y);
}

Output:

Sunday = 0

Friday = 5

Enumerators can have initializers to override the default values.

Example:

enum Days 
{ 
     Sunday=1, 
     Monday, 
     Tuesday, 
     Wednesday, 
     Thursday, 
     Friday, 
     Saturday 
};

Now in this case the output of the above code will be

Sunday = 1

Friday = 6

By default the underlying type of each element in the enum is int. You can specify another integral numeric type by using a colon, as shown in following example.

enum Months : byte 
{ 
     Jan, 
     Feb, 
     Mar, 
     Apr, 
     May, 
     Jun, 
     Jul, 
     Aug, 
     Sep, 
     Oct, 
     Nov, 
     Dec 
};

Conversions of enum in c#:

For demonstrating conversion I’m going to use the following enumeration:

enum Days 
{ 
     Sunday=1, 
     Monday, 
     Tuesday, 
     Wednesday, 
     Thursday, 
     Friday, 
     Saturday 
};

Converting enum to string:

Days day = Days.Monday;
string sDay = day.ToString();

In the above code sDay will hold “Monday” as a value.

Converting enum to int:

Days day = Day.Monday;
int iDay = (int)day;

After conversion iDay will be equal to 2.

Converting string to enum:

// Possible user input:
string sDay = "Monday";

// Try to convert the string to an enum:
Days day = (Days)Enum.Parse(typeof(Days), value);

Note: You can cause exceptions to be raised when parsing enums. When the contents of the string you try to parse is not represented in the enum, you must handle the exception. Handling exceptions is often one of the most important parts of your program.

Converting int to enum:

// Possible user input:
int iDay = 1;

// Try to convert the string to an enum:
Days day = (Days)iDay;

Hope, the above information on enum will be helpful. If you find any mistakes or want to give your suggestions please feel free to comment on the post and help me serve better.