Enumeration

Enumeration is a way of defining your own type that can accept a predefined set of values identified by names. For example, you want a variable that can only store directions such as east, west, north, and south. You can define an enumeration named Direction and declare all the possible values it can have inside the enumeration body. Let’s look at the syntax for an enumeration.

enum enumName
{
   value1,
   value2,
   value3,
   .
   .
   .
   valueN
}

We used the enum keyword then the name of the enumeration. As a practice in the C# world, enumeration names uses Pascal Casing. Following is the body of the enum. Inside it are the values identified by names. Here is how our Direction enumeration would look like.

enum Direction
{
   North,
   East,
   South,
   West
}

By default, the values that an enumeration can store are of type int. For example, North is mapped to the value of 0 and each subsequent values is 1 greater than the last value. So East has a value of 1, South has 2, and West has 3. You can modify this flow by specifying a specific value like this:

enum Direction
{
   North = 3,
   East = 5,
   South = 7,
   West = 9
}

This time, the North has a value of 3, East has a value of 5, South has a value of 7 and West contains a value of 9. If for example you don’t assign a value for a value, then an underlying value is given to it automatically.

enum Direction
{
   North = 3,
   East = 5,
   South,
   West
}

After East, we didn’t assign a value to South, therefore, it’s value will be 1 greater than the value of 5 which is 6 and West will have a value of 1 greater than South, which is 7. You can also assign identical values for the enumeration items.

enum Direction
{
   North = 3,
   East,
   South = North,
   West
}

Can you guess their values now? The values for NorthEastSouth, and West are 3, 4, 3, 4 respectively. We assign 3 to North so Eastwill have a value of 4. Then we assign the value of South with the value of North which is 3. If South has now a value of 3, then the next one which is West will have a value of 4.

Be cautious though when using this technique. If you assign the value of North to South, then these two directions would be equal and you can’t walk to North and South at the same time. But in some occasions, you might find using this technique if it is appropriate.

If you don’t want the values of your enumeration items to be int (which is the default). For example, you can use byte as the type of the enumeration items.

enum Direction : byte
{
   North,
   East,
   South,
   West
}

The byte data type can only hold a value of up to 255 so the number of values you can add to the enumeration is quite limited.  Let’s see how to use an enumeration inside a C# program.

using System;

namespace EnumerationDemo
{
    enum Direction   
    {                
        North = 1,   
        East,        
        South,       
        West         
    }                


    public class Program
    {
        public static void Main()
        {
            Direction myDirection;


            myDirection = Direction.North;

            Console.WriteLine("Direction: {0}", myDirection.ToString());
        }
    }
}

Example 1 – Enumeration Demo

Direction: North

First, we created our enumeration (lines 5-11). Note that we placed our  enumeration outside class Program. Doing so will  make our enumeration available throughout the program. You can also place the  declaration of the enumeration inside a class to make it only available inside  that class.

class Program
{
    enum Direction
    {
          //Code omitted
    }

    static void Main(string[] args)
    {
          //Code omitted  
    }
}

Continuing with our program in Example 1. Inside the enumeration are the four possible values and each of them is assigned with value 1 to 4.  Line 17 declared our          variable that will store a Direction value. We follow this syntax:

enumType variableName;

where the enum type is the Enumeration Type such as Direction and the name of a particular enumeration value. After that, we assign a value to myDirection variable  (line 19). We used this syntax:

variable = enumType.value;

We wrote the Enumeration type then a period and then the value from that particular enumeration type such as North. You can initialize a variable right away in its declaration like this:

Direction myDirection = Direction.North;

We then print the value of myDirection using Console.WriteLine()  (line 21). Notice that we use the method ToString() to convert the value into its  string equivalent.

Enumeration is such a handy tool that it appears ubiquitously in the .NET Framework. Imagine if there is no enumeration, you have to memorize numbers instead of words since enumeration values are actually numbers being aliased with names defined by you or by other people.  You can also perform bitwise operations on numerous .NET enumerations to produce  some exciting results as you will see in some of the tutorials in this site. Enumeration variables can  also be converted to other types such as int or string or convert a stringvalue to an enumeration equivalent.