Reader Level:
ARTICLE

Enumeration In C#

Posted by Sandeep Singh Shekhawat Articles | Articles C# January 02, 2013
In this article I explain how to use enum, create an enum and get values from an enum with their enumeration list.
  • 0
  • 0
  • 5024

Introduction

The idea behind enumerated types is to create new data types that can take on only a restricted range of values. Moreover, these values are all expressed as constants rather than magic numbers, in fact, there should be no need to know the underlying values. The names of the constants should be sufficient for the purposes of comparing values. 

Enumerations are a user-defined Value Type; in other words an enumeration allocates memory in a stack and works on a Last In First Out approach. They are strongly typed constants, which allow us to assign symbolic names to the integral values. The phrase strongly typed means that an enumeration of one type can't be assigned to the enumeration of another type, even though the values of their members are the same.

Creating an Enumeration

  1. The enum keyword is used to declare an enumeration. It is an alias for the System.Enum class.

  2. A distinct type consisting of a set of named constants called the enumerator list.

  3. Every enumeration type has an underlying type; the underlying type can be an integral type but not char.

  4. The default underlying type of the enumeration elements is int.

  5. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

  6. Each element in the enumeration list is separated by a comma (,).

Syntax
 
          enum enum_Name
           {
               element1,
               element 2
           }

Example

Create an Enumeration

public enum Color
        {
            Red,
            Green,
            Yellow
        }

Here Color is an enumeration that has the three elements Red, Green and Yellow. Red has the default value, 0 and the value of the other elements are incremented by 1; in other words it is similar to:

public enum Color
        {
            Red = 0,
            Green = 1,
            Yellow = 2
        }

But when we do assign a value to Red, like Red = 3 and leave Green and Yellow without a value then Green will have Green = 4 and Yellow = 5.

public enum Color
        {
            Red = 3,
            Green,
            Yellow
        }

Sample use of this Color enumeration:

static void Main(string[] args)
        {
            int green = (int)Color.Green;
            int yellow = (int)Color.Yellow;
            Console.Write("Value of Green = {0}",green);
            Console.Write("\n Value of Yellow = {0}", yellow);
            Console.Read();
        }

1.PNG
 

But we assign a new value to another element and leave the last one; in that case the last element value will be 1 greater than its previous element not from the first element.

public enum Color
        {
            Red = 3,
            Green=1,
            Yellow
        }

In this case Yellow will have the value 2.

static void Main(string[] args)
        {           
            int yellow = (int)Color.Yellow;           
            Console.Write("Value of Yellow = {0}", yellow);
            Console.Read();
        }

2.PNG

As in the above example we converted the enum element values to int; in the same way we can convert an int value to enum.

      public enum Color
        {
            Red = 1,
            Green,
            Yellow
        }
        static void Main(string[] args)
        {
            int colorValue = 2;
            Color col = (Color)colorValue;
            Console.Write("Value of Color is {0}", col);
            Console.Read();
        }

3.PNG
 

Get all enum elements with their values:

public enum Color
        {
            Red = 1,
            Green,
            Yellow
        }
        static void Main(string[] args)
        {
            Array enumColorValue = Enum.GetValues(typeof(Color));
            foreach (int enumValue in enumColorValue)
            {
                string colorValue = Enum.GetName(typeof(Color), enumValue);
                Console.WriteLine("Name: {0} and value :{1}",colorValue , enumValue);
            }          
            Console.Read();
        }

4.PNG

COMMENT USING

Trending up