Facts of Enumerations in C#


Introduction

You must be aware about enumerations (or simply enum), but there are many things which we should keep in mind always. Let's begin from basics. 
C# has two fundamental types, value types and reference types. A value type variable holds values directly on stack whereas 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 day's name. We could use the integer 0, 1, 2 and so on to represent Sunday, Monday, Tuesday, etc respectively. This system will work cool but it is not much intuitive or say it is not 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, if we declare an integer variable named day, then there is nothing which stops assigning any legal integer value apart from 0, 1, …., 5, 6. Here C# offers much better experience for us by using enumerations (enum) where day'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? 

enum keyword is used to declare an enumeration, this 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 put above example in program to look 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 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 base-type option is 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 all enum list by using 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.

Comments

Popular posts from this blog

Customize User's Profile in ASP.NET Identity System

Migrating database from ASP.NET Identity to ASP.NET Core Identity