Facts of Enumerations in C#


Introduction 

You must be aware of enumerations (or simply enum), but there are many things we should always keep in mind about them. Let's begin with the basics. 

C# has two fundamental types, value types and reference types. A value type variable holds values directly on the stack whereas a reference type variable holds a reference to a variable or object on the heap. 

In value types, C# has enumerations and structures. 

Why we use Enumerations? 

Okay, suppose we want to work with name of the day of the week. We could use the integers 0, 1, 2 and so on to represent Sunday, Monday, Tuesday, etc. respectively. This system will work but it is not very intuitive, or say it is not a robust solution because if we use the integer value 0 in code, it would not be obvious that a particular 0 represents Sunday. One more thing is if we declare an integer variable named day, then there is nothing preventing someone from assigning any legal integer value apart from 0, 1, ...., 5, 6. Here C# offers much better experience for us by using enumerations (enum) where the weekday's values are limited to a defined set. An enumeration type provides an efficient way to define a set of named integral constants that may be assigned to a variable. For example, assume that we have to define a variable whose value will represent a day of the week. There are only seven meaningful values which that variable will ever store. To define those values, we can use an enumeration type, which is declared by using the enum keyword. 

How to use Enumerations? 

The enum keyword is used to declare an enumeration consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type (byte, sbyte, short, ushort, int, uint, long, or ulong) except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. 

For example: 

enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; 

Let's use the above example in a program to see at its integral type factor. 

using System;
public
 class EnumTest
{
    enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    static void Main()
    {
        int x = (int)Days.Sunday;
        int y = (int)Days.Saturday;
        Console.WriteLine("Sunday = {0}", x);
        Console.WriteLine("Saturday = {0}", y);
        Console.ReadKey();
    }
}
 

Output:

Sunday = 0

Saturday = 6 

In the above program, I'm casting (even it is important to cast always) the Days.Sunday and Days.Saturday to integer type and assigning it to new x and y integer variables. 

Look at another example where, Sunday begins with 2, then Monday will be 3, Tuesday will be 4,…, and Saturday will be 8. Enumerators can have initializes to override the default values. 

For example: 

using System;
public
 class EnumTest
{
    enum Days { Sunday=2, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    static void Main()
    {
        int x = (int)Days.Sunday;
        int y = (int)Days.Saturday;
        Console.WriteLine("Sunday = {0}", x);
        Console.WriteLine("Saturday = {0}", y);
        Console.ReadKey();
    }
}
 

Output:

Sunday = 2

Saturday = 8 

Look at one more example, which has a base-type option, used to declare an enum whose members are of the type long. Notice that even though the underlying type of the enumeration is long, the enumeration members must still be explicitly converted to type long using a cast. 

For example: 

using System;
public
 class EnumTest
{
    enum Range : long { Num1 = 43567883635L, Num2 = 310L };
    public static void Main()
    {
        long x = (long)Range.Num1;
        long y = (long)Range.Num2;
        Console.WriteLine("Num1 = {0}", x);
        Console.WriteLine("Num2 = {0}", y);
        Console.ReadKey();
    }
}
 

Output:

Num1 = 43567883635L

Num2 = 310L 

We can print an all enum list as in the following example: 

using System;
public
 class EnumTest
{
    enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
    static void Main()
    {
        foreach (int i in Enum.GetValues(typeof(Days)))
        Console.WriteLine(i);
        Console.ReadKey();
    }
}
 

Output:

0

1

2

3

4

5

6 

That's all about the enumerations. Thanks for reading.


Similar Articles