C# Enum Tutorial For Beginners With Examples

An enum consists of a set of named constants. Using enums, symbolic names are assigned to integral constants. This type of set is known as an enumeration list.
An enum is useful when there is a need to group related constants. For example an enum called as WeekDay can be created to store all the weekdays of a week. Similarly an enum called as Color can be created to store different type of color constants.

Create An Enum

The enum keyword is used to create enum types.
In the below example an enum called as Color is used to store three different constants i.e. 1, 2 and 3. These constants are named as Red, Green and Blue respectively.
    enum Color
    {
        Red = 1,
        Green = 2,
        Blue = 3
    }
        
In the above example it is not mandatory to assign integral constant values to the names Red, Green and Blue. So it is perfectly valid to create the above enum as below:
    enum Color
    {
        Red,
        Green,
        Blue
    }
        
In the above example by default the first enumeration is assigned a value of zero and the subsequent enumerations are increased by one.
The last named constant in an enumeration list may or may not have a comma. Both the cases are valid in C#.

Data Type of an Enum

By default the underlying data type of an enum is int. However, it is possible to override the default type with another allowed type. The types allowed for an enum are byte, sbyte, ushort, short, uint, int, ulong and long.
As shown in the below example the default int type has been overridden with the the typeuint.
    enum HouseNumber:uint
    {
        StartNumber = 100,
        EndNumber = 4000000,
    }
        

How to use an Enum?

Enum constants are referred by prefixing the constant name with the dot(.) operator and the enum name.
In the below example the named constant Green is referred as Color.Green. To retrieve the integral constant associated with Color.Green casting is done using the int data type.
    enum Color
    {
        Red,
        Green,
        Blue
    }

    class Program
    {
        static void Main(string[] args)
        {
            SetColor(Color.Green);
            Console.ReadLine();
        }

        static void SetColor(Color newColor)
        {
            switch (newColor)
            {
                case Color.Blue:
                    Console.WriteLine("The constant value for " + newColor + " : " +  (int)newColor);
                    break;
                case Color.Green:
                    Console.WriteLine("The constant value for " + newColor + " : " + (int) newColor);
                    break;
                case Color.Red:
                    Console.WriteLine("The constant value for " + newColor + " : " + (int)newColor);
                    break;
                default:
                    break;
            }   
        }
    }
        
Output:
The constant value for Green : 1
Using enum makes the code more readable and self-documenting.