22 - Enums in C#

22.1 Introduction to Enums

Enumeration is a value type data, which means that enumeration contains its own values. They are strong typed constants. They cannot be inherited in an application or pass inheritance. They are unique types that allow users to assign the names to the integral values. They are strongly typed means the value cannot be assigned to the members of same or different data type. Enumerations allow user to assign symbolic names to integral constants.

Consider an example where you want to write a program to display weekdays in a program. User can use integers as 0,1, and 2 to represent Monday, Tuesday, and Wednesday respectively. It is not convenient to use such representation. If the integer value 0 is used for Monday, it means that 0 would represent a Monday.

To overcome such problem, enumerations are used. The enum keyword is used to create enumeration in C#.

22.2 Creation of Enum

The Enum is of data type integer but it can be modified to any of the base types by specifying the type in the declaration. The valid data types for enum are long, ulong, uint, short, ushort, byte and sbyte.

The general format of enumeration is as shown below:

    enum name { enumeration list};

To declare an enumeration type called Days, where the values are restricted due to the symbolic names of the weekdays, use the following code:

    enum Days { Mon , Tue, Wed, Thu, Fri, Sat, Sun };

The names of days must appear within braces and must be separated by commas. The enumeration type gives the numeric value to every element. By default, the sequence of enumeration starts from 0 for the first element and increments by 1 for every subsequent element.

User can specify one or more of the symbols by using an initializer. The following can be achieved by using the symbol of equal sign and an integer value.

The snippet for the following is as shown below:

    enum apple { GoldenDel, RedDel = 10, Cortland, GreenApple };

User can iterate through the elements of the collection without the number of elements in it through foreach statement. The enumerator is used to achieve the task. The array or collection implements the IEnumerable interface with the GetEnumerator() method. The method returns an enumerator implementing the IEnumerable interface.

The list of properties and methods used by the IEnumerator interface are as mentioned below:

IEnumerator Interface

Properties and Methods

Description

MoveNext()

The method moves to the next element of the collection and returns true if there is an element. If there are no elements present, false value is returned

Reset()

The method repositions the cursor to the beginning of the collection

Current

The property returns the element depending on the cursor position

22.3 Example using Enum

After declaring an enumeration type, user can add any data type as shown in the following code:

class EnumTest
{
    enumDays { Mon, Tue, Wed, Thu, Fri, Sat, Sun };

    static void Main ( string[ ] args )
    {
        int First_Day = ( int ) Days.Mon;
        int Last_Day = ( int ) Days.Sun;
        Console.WriteLine(“Mon ={0}”, First_Day );
        Console.WriteLine(“Sun = {0}”, Last_Day );
        Console.Read();
    }
}

 

The output for the code is as shown below:

In the preceding code, Days have been declared as an enum data type. This enum has symbolic names, which appear within braces. In the class, EnumTest the main() method displays the values of enum Days. The local variables First_Day and Last_Day hold the value of enum and display the values for Mon and Sun as output.

Consider another example demonstrating enumeration in C# as shown below:

class Program
{
    enum Status { low, medium, high, superior };
    
    static void Main ( string[ ] args )
    {
        Status value = Status.high;
        if ( value == Status.high)
        {
            Console.WriteLine(“Top reference”);
        }
        else if ( value == Status.low)
        {
            Console.WriteLine(“Less reference”);
        }
        Console.Read();
    }
}

 

The output for the code is as shown below:

In the above code, the enumeration as Status is declared. It contains elements defining the priority. In the class Program, the main() method is used to access the members of the enumeration.

Like us on Facebook