Enum Type in C #

Sometimes you need to create a set of symbolic names that map to known numeric values that comes from data source (database, file…etc). In this case enumerations are very useful. C# supports enumerations by using Enum keyword. Enum is like key value pair.

 

For example we have an application for employees and there are different levels in employees and different numeric value in database for each role. We can convert each numeric value into symbolic name through enumeration concept.

 

Before going to example, first we have to know how enum stores each value. By default enum stores each value as integer data type in memory in sequential order.

 

enum EmpRole
{
    CEO,   // it stores as 0 in memory
    Director, // it stores as 1 in memory
    Manager, // it stores as 2 in memory
    Analyst // it stores as 3 in memory
}

 

Changing the Underlying storage for enum:

 

If you want to store enum values in other than int data type you can do it. If you are building application for low memory device, you need to save memory where ever it possible. In this case you need use byte data type instead of int data type. You can change underlying storage of enum as shown below.

 

//here EmpRole maps underlying byte instead of int
enum EmpRole : byte
{
    CEO,   // it stores as 0 in memory
    Director, // it stores as 1 in memory
    Manager, // it stores as 2 in memory
    Analyst // it stores as 3 in memory
}

 

If you want to change starting value of enum, you can change it by assigning value to first string enum as shown below.

 

enum EmpRole
{
    CEO = 10,   // it stores as 10 in memory
    Director, // it stores as 11 in memory
    Manager, // it stores as 12 in memory
    Analyst // it stores as 13 in memory
}

 

If you don’t want to store enum in sequential order, you can change it by assigning values for each enum string.

 

enum EmpRole
{
    CEO = 102,   // it stores as 102 in memory
    Director = 200, // it stores as 200 in memory
    Manager = 60, // it stores as 60 in memory
    Analyst = 70 // it stores as 70 in memory
}

 

Now we will go into small application where we are converting database numeric values into enum ype. Here I am taking default data type for storage mechanism (i.e., int data type)

 

class Program
{
        static void Main(string[] args)
        {
            EmpRole empType;
            int empRoledbValue;
            //here assign role value from data base to empRoledbValue
            //I am directly assigning the value for empRoledbValue
            empRoledbValue = 2;

            //casting the int value to EmpRole enum type
            empType = (EmpRole)empRoledbValue;

            Console.WriteLine("Employee Type: " + empType.ToString());
            Console.ReadLine();

        }
}
enum EmpRole

{
        CEO,   // it stores as 0 in memory
        Director, // it stores as 1 in memory
        Manager, // it stores as 2 in memory
        Analyst // it stores as 3 in memory

}

 

As shown in above code, we are casting the int value to enum type EmpRole. Then it will check corresponding enum value in memory and displays enum string.

 

                                                                                                              EnumExample.zip (20.27 kb)