Enums in C# with Example

C#Enums stands for enumerations.  It is basically a set of integral numbers.  Enums are value types.  Enums make your program more readable as well as more maintainable.

I know since you are new to C#, the above lines do not make sense to you at all.  Lets understand enums with a very basic example.  Whenever a developer creates a program, he distributes it with certain restrictions.  There are 3 modes in that program (Trial, Active, & Expired).  Trial users can use the program for a fixed number of days.  Active users can use the program without any restrictions.  Expired users can no longer user the program since their subscription is expired.

Now, we can create a custom function to know about the user’s subscription and run the program in that mode.   We can return integer values after finishing our checks like this:

  • Trial -> 0
  • Active -> 1
  • Expired -> 2

So, based on these integer values, we will decide in which mode the program should run.  I know this sounds easy here, but there is a catch.  We can add more modes to it like Professional Edition, Developer Edition, Standard Edition etc.  Do you think, you or your team members can remember integral values of numerous modes during development?  Of course, not!

In such situation, enums make the work easy for you and your team members.  I know you are thinking why not we just return a string and compare it with another?  I hope you know that a slight change in case of a character or any kind of additional space in the string can give wrong result.  To create an enum, we make use of enum keyword.  Example is given below:

using System;
using System.IO;


namespace Hello_World
{

    //Mode Enum
    public enum mode
    {
        Trial,
        Active,
        Expired
    }

    //Customer Class
    public class customer
    {
        public string name { get; set; }
        public mode acess { get; set; }

    }

    class Program
    {
        static void Main(string[] args)
        {
            customer[] custs = new customer [5];

            custs[0] = new customer { name = "Robert", acess = mode.Active };
            custs[1] = new customer { name = "Albert", acess = mode.Active };
            custs[2] = new customer { name = "Ronaldo", acess = mode.Trial };
            custs[3] = new customer { name = "Anita", acess = mode.Active };
            custs[4] = new customer { name = "Susan", acess = mode.Expired };

            foreach(customer c in custs)
            {
                Console.WriteLine("Name: {0}  Mode: {1}", c.name, GetMode(c.acess));
            }

        }

        //Method to return mode
        public static string GetMode(mode user)
        {
            switch(user)
            {
                case mode.Active:
                    return "Active";

                case mode.Trial:
                    return "Trial";

                case mode.Expired:
                    return "Expired";
                default:
                    return "Unknown";
            }
           
        }

    }
}

Enums are strongly typed constants and the default underlying data type is integer.  You cannot assign an integer value directly to an enum without type casting and vice versa.  If you have 2 enums, then an enum of one type cannot to be assigned to enum of another type directly even though the underlying values of members are same.  This is only possible with explicit type cast.  Example is given below.

using System;
using System.IO;


namespace Hello_World
{

    //Mode1 Enum
    public enum mode1
    {
        Trial,
        Active,
        Expired
    }

    //Mode2 Enum
    public enum mode2
    {
        Trial,
        Active,
        Expired
    }

    class Program
    {
        static void Main(string[] args)
        {

            //Type casting - int to enum
            mode1 mode = (mode1) 2;            
            Console.WriteLine(mode);

            //Type casting - enum to int
            int number = (int)mode;
            Console.WriteLine(number);

            //Type casting - mode2 to mode1
            mode1 custom = (mode1)mode2.Active;
            Console.WriteLine(custom);
        }

    }

}


To get default integral values, we make use of GetValues() static method which resides in Enum class.  Due to strongly typed nature, an explicit cast of int is required.  Similarly, to get names, we make use of GetNames(), but an explicit cast of string is not required in this case.  The point to remember here is, enum in small case is a keyword to create enums.  Enum with E in upper case is for Enum class.  Example is given below

using System;
using System.IO;


namespace Hello_World
{

    //Mode Enum
    public enum mode
    {
        Trial,
        Active,
        Expired
    }


    class Program
    {
        static void Main(string[] args)
        {

            //Getting underlying int values
            int[] values = (int[])Enum.GetValues(typeof(mode));

            foreach (int value in values)
            {
                Console.WriteLine(value);
            }


            //Getting names
            string[] names = Enum.GetNames(typeof(mode));

            foreach (string name in names)
            {
                Console.WriteLine(name);
            }
        }

    }

}


You can also change default integer values of elements in enums and can assign custom values to each of them.  The integral values of elements get incremented by 1 automatically.  This means if value of first element is 10, then next element value will be 11.  However, by default, first element value is always 0.  Example is given below:

using System;
using System.IO;


namespace Hello_World
{

    //Mode Enum
    public enum mode
    {
        Trial = 5,
        Active = 8,
        Expired
    }


    class Program
    {
        static void Main(string[] args)
        {

            //Getting underlying int values
            int[] values = (int[])Enum.GetValues(typeof(mode));

            foreach (int value in values)
            {
                Console.WriteLine(value);
            }
        }

    }

}

You can also change default underlying data type of enums from int to any other data type like short, long etc.  Example is given below:

using System;
using System.IO;


namespace Hello_World
{

    //Mode Enum of Short Datatype.
    public enum mode : short
    {
        Trial,
        Active,
        Expired
    }


    class Program
    {
        static void Main(string[] args)
        {

            //Getting underlying int values
            short[] values = (short[])Enum.GetValues(typeof(mode));

            foreach (short value in values)
            {
                Console.WriteLine(value);
            }
        }

    }

}