Enumeration Type in C#

The enumeration (or enum) is the value of a data type in C #. It is mainly used to assign string names or values ​​to integral constants that make it easier to read and maintain programs

Syntax:

enum variable
{
        // declaration            
}

Use of enumeration

In C# enumeration (enum) are defined with the separation of a comma(,).

Example:

enum weekday
{
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Satureday,
	Sunday

}
class Entry
{
	static void Main(string[] args)
	{
		Console.WriteLine("The value of Monday in weekday " + "enum is " + (int)weekday.Monday);
		Console.WriteLine("The value of Tuesday in weekday " +"enum is " + (int)weekday.Tuesday);
		Console.WriteLine("The value of Wednesday in weekday " + "enum is " + (int)weekday.Wednesday);
		Console.WriteLine("The value of Thursday in weekday " +"enum is " + (int)weekday.Thursday);
		Console.WriteLine("The value of Friday in weekday " +"enum is " + (int)weekday.Friday);
		Console.WriteLine("The value of Saturday in weekday " + "enum is " + (int)weekday.Satureday);
		Console.WriteLine("The value of Sunday in weekday " + "enum is " + (int)weekday.Sunday);
	}
}

Output:

The value of Monday in weekday enum is 0
The value of Tuesday in weekday enum is 1
The value of Wednesday in weekday enum is 2
The value of Thursday in weekday enum is 3
The value of Friday in weekday enum is 4
The value of Saturday in weekday enum is 5
The value of Sunday in weekday enum is 6

In the above example, we have defined the enumeration of a weekday. Which contain 7 constants.

Use of enumeration in switch

We can use the enumeration in a switch statement for achieving the corresponding values.

Example:

using System;
enum weekday
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Satureday,
    Sunday

}
class Entry
{
    static void Main(string[] args)
    {
        weekday weekday = weekday.Friday;
        switch (weekday)
        {
            case weekday.Friday:
                Console.WriteLine("Friday");
                break;
            case weekday.Sunday:
                Console.WriteLine("Sunday");
                break;
            case weekday.Monday:
                Console.WriteLine("Monday");
                break;
            default:
                Console.WriteLine("your are in default");
                break;
        }
    }
}

Output:

Friday

Usually, in the enumeration, the first item has the value 0 second item has the value 1, and so on.