C Sharp Enum

C# Enum

Also known as enumeration, enum in C# is used to store a set of named constants (also known as enumerators). Constant can be anything like seasons, days, months, size, etc. Declared within or outside class and structs, enumerators have default values starting from 0 and are incremented one by one. The default value can be changed. With a fixed set of constants, enum supports advantages like type safety improvement and can also be traversed.

Example 1:

using System;  
public class Example  
{  
    public enum Month { January, February, March, April, May, June, July, August, September, October, November, December }    
 
    public static void Main()  
    {  
        int a = (int)Month.March;  
        int b = (int)Month.July;
        int c = (int)Month.October;  
        Console.WriteLine("March = {0}", a + 1);  
        Console.WriteLine("July = {0}", b + 1);  
        Console.WriteLine("October = {0}", c + 1);  
    }  
}

Output:

Explanation:

In the above example, we are displaying a simple example of C# enum. Here, we are displaying the enum example for Months in C#.

Example 2: Changing start index.

using System;  
public class Example  
{  
    public enum Month { January = 1, February, March, April, May, June, July, August, September, October, November, December }    
 
    public static void Main()  
    {  
        int a = (int)Month.March;  
        int b = (int)Month.July;
        int c = (int)Month.October;  
        Console.WriteLine("March = {0}", a);  
        Console.WriteLine("July = {0}", b);  
        Console.WriteLine("October = {0}", c);  
    }  
}

Output:

Explanation:

In the above example, we are displaying the enum example for changing the start index in C#. Here, we are displaying the enum example for Months in C#.

Example 3:

using System;  
public class Example  
{  
    public enum Days { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
 
    public static void Main()  
    {  
        int a = (int)Days.Sunday; 
        int b = (int)Days.Tuesday;  
        int c = (int)Days.Thursday;  
        int d = (int)Days.Saturday;  
        Console.WriteLine("Sunday = {0}", a); 
        Console.WriteLine("Tuesday = {0}", b);  
        Console.WriteLine("Thursday = {0}", c);  
        Console.WriteLine("Saturday = {0}", d);  
    }  
}

Output:

Explanation:

In the above example, we are displaying the enum example for Days in C#.

Example 4: Traversing all values using getNames() method in C#.

using System;  
public class Example  
{  
    public enum Days { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
 
    public static void Main()  
    {  
         foreach (string x in Enum.GetNames(typeof(Days)))  
        {  
            Console.WriteLine(x);  
        }  
    }  
}

Output:

Explanation:

In the above example, we are traversing all values using the getNames() method in C#. Here, we are displaying the enum example for Days in C#.

Example 5: Traversing all values using getValues() method in C#.

using System;  
public class Example  
{  
    public enum Days { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
 
    public static void Main()  
    {  
         foreach (Days x in Enum.GetValues(typeof(Days)))  
        {  
            Console.WriteLine(x);  
        }   
    }  
}

Output:

Explanation:

In the above example, we are traversing all values using the getValues() method in C#. Here, we are displaying the enum example for Days in C#.

Please Share