Close
Close

Enum in C#


Enumeration (enum) is a value type (same as structure). There is no such specific use of enum, we use it just to make our codes neat and more readable. We can write C# programs without using enumerations also.

Enums are like variables which store integer values but are grouped together and given a name. For example, Summer, Spring, Winter and Autumn are the names of four seasons. We can give integer values to these seasons and group them together to make an enum Season.

C# Defining an Enum


We use the enum keyword to define an enum. Each element of an Enum is separated by a comma.

enum EnumName
{
    element1,
    element2,
    element3,
    element4,
};

Now let's define an enum of the above example of seasons.

enum Season
{
    Summer,
    Spring,
    Winter,
    Autumn
};

Here, we have defined an enum with name Season and Summer, Spring, Winter and Autumn as its elements.

By default, the first element takes a value of 0, second 1, and so on. So, here the value of Summer would be 0, Spring 1, Winter 2 and Autumn 3.

However, we can also start the values from where we want. We will just give a value to the first element. For example, if we give a value of 5 to Summer, the value of Spring would be 6, Winter 7 and Autumn 8.

enum Season
{
    Summer = 5,
    Spring,
    Winter,
    Autumn
};

We can also give different values to each element.

enum Season
{
    Summer = 5,
    Spring = 10,
    Winter = 15,
    Autumn = 20
};

Here, each element has a different value.

C# Accessing Elements of Enum


To access an element of an enum, we write EnumName.ElementName, where EnumName is the name of the enum and ElementName is the name of the element.

To get the integer value, we need to cast it into integer - (int)EnumName.ElementName

Let's take few examples.

using System;

enum Season
{
  Summer,
  Spring,
  Winter,
  Autumn
}

class Test
{
  static void Main(string[] args)
  {
    int a = (int)Season.Summer;
    int b = (int)Season.Spring;
    int c = (int)Season.Winter;
    int d = (int)Season.Autumn;

    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
  }
}
Output
0
1
2
3

You can see that by default, values started from 0.

using System;

enum Season
{
  Summer = 5,
  Spring,
  Winter,
  Autumn
}

class Test
{
  static void Main(string[] args)
  {
    int a = (int)Season.Summer;
    int b = (int)Season.Spring;
    int c = (int)Season.Winter;
    int d = (int)Season.Autumn;

    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
  }
}
Output
5
6
7
8

Here, we have given Summer a value of 5. So, the values started from 5.

using System;

enum Season
{
  Summer = 5,
  Spring = 10,
  Winter = 15,
  Autumn = 20
}

class Test
{
  static void Main(string[] args)
  {
    int a = (int)Season.Summer;
    int b = (int)Season.Spring;
    int c = (int)Season.Winter;
    int d = (int)Season.Autumn;

    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.WriteLine(c);
    Console.WriteLine(d);
  }
}
Output
5
10
15
20

Here, each element of the enum is given a different value.

C# GetNames


GetNames is a method avialable in Enum which returns an array of strings of all elements. We pass an enumeration type to the method and to do so, we use typeof operator. typeof operator gives us the type of any data. Since we need to pass the type to the GetNames method, we would pass Enum.GetNames(typeof(Season)). Let's see the below example.

using System;

enum Season
{
  Summer,
  Spring,
  Winter,
  Autumn
}

class Test
{
  static void Main(string[] args)
  {
    foreach(string i in Enum.GetNames(typeof(Season)))
      Console.WriteLine(i);
  }
}
Output
Summer
Spring
Winter
Autumn

C# GetValues


It is similar to GetNames but returns an array of values of the elements.

using System;

enum Season
{
  Summer,
  Spring,
  Winter,
  Autumn
}

class Test
{
  static void Main(string[] args)
  {
    foreach(int i in Enum.GetValues(typeof(Season)))
      Console.WriteLine(i);
  }
}
Output
0
1
2
3

C# GetName

GetName is used to get the name of an element of an enum having specific value.

using System;

enum Season
{
  Summer,
  Spring,
  Winter,
  Autumn
}

class Test
{
  static void Main(string[] args)
  {
    Console.WriteLine(Enum.GetName(typeof(Season), 3));
  }
}
Output
3

Here, we have used GetName(typeof(Season), 3) to get the name of the element in the enum Season having a value of 3.


Ask Yours
Post Yours
Doubt? Ask question