Understanding Enums in C# (Part – 2)

Understanding Enums in C# (Part – 2)

In this article, we will continue our discussion for Understanding Enums in C#. Please read our previous article where we discuss the basics of Enums. We are going to work with the same example that we started in our previous article.

In our previous article, we wrote one point that Enum of one type cannot be implicitly assigned to an enum of another type even though the underlying value of their members are same. In such cases, an explicit cast is required as shown in the below example

namespace EnumsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // This following line will not compile. 
            // Cannot implicitly convert type 'Season' to 'Gender'. 
            // An explicit conversion is required.
            // Gender gender = Season.Winter;

            // The following line comiples as we have an explicit cast
            Gender gender = (Gender)Season.Winter;
        }
    }

    public enum Gender : int
    {
        Unknown = 1,
        Male = 2,
        Female = 3
    }
    public enum Season : int
    {
        Winter = 1,
        Spring = 2,
        Summer = 3
    }
}
Understanding GetValues() and GetNames() methods

The enum keyowrd (all small letteres) in C# is used to create enumerations whereas the Enum class in C# contains the static GetValues() and GetNames() methods which can be used to list the Enum underlying type values and Names. Let us understand this with an example.

namespace EnumsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Values = (int[])Enum.GetValues(typeof(Gender));
            Console.WriteLine("Gender Enum Values");
            foreach (int value in Values)
            {
                Console.WriteLine(value);
            }

            Console.WriteLine();
            string[] Names = Enum.GetNames(typeof(Gender));
            Console.WriteLine("Gender Enum Names");
            foreach (string Name in Names)
            {
                Console.WriteLine(Name);
            }
       Console.ReadKey();
        }
    }

    public enum Gender : int
    {
        Unknown = 1,
        Male = 2,
        Female = 3
    }
}

When we run the application, it gives us the following output.

Understanding Enums in C#

The Enums cannot be derived from other enums.

Let us understand this with an example. Here we have two enums InitialDays and Weekdays and we are trying to inherit the WeekDays enums from the InitialDays enum as shown in the below image.

Understanding Enums in C#

When we compile the application, it gives us the below error.

Understanding Enums in C#

So, we cannot derive enums from another enum. Well, if enums cannot be derived from enums then can be a class derived from enums? Let’s find it out with an example.

using System;
namespace EnumsDemo
{
    class Program : WeekDays
    {
        static void Main(string[] args)
        {
            Console.WriteLine((int)WeekDays.Monday);
            Console.WriteLine((int)WeekDays.Sunday);
            Console.WriteLine((int)WeekDays.Tuesday);
            Console.WriteLine((int)WeekDays.Wednesday);
            Console.WriteLine((int)WeekDays.Thursday);
            Console.WriteLine((int)WeekDays.Friday);
            Console.WriteLine((int)WeekDays.Saturday);
            
            Console.ReadKey();
        }
    }
    
    public enum WeekDays
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }
}

When we compile the application, it gives us the following error.

Understanding Enums in C#

The above error states that classes cannot be derived from enums. This is because Enums are treated as sealed classes and hence all rules that are applicable to sealed classes also apply to enums.

Duplicate Enum Members:

Let us try to create two enum members with the same name as shown in the below example.

using System;
namespace EnumsDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine((int)Gender.Unknown);
            Console.WriteLine((int)Gender.Male);
            Console.WriteLine((int)Gender.Female);           
            Console.ReadKey();
        }
    }
    
    public enum Gender
    {
        Unknown,
        Male,
        Female,
        Unknown
    }
}

When we compile the program, it gives us the following error.

Understanding Enums in C#

The Enums are like classes, so we cannot define two members with the same name.

In the next article, I will discuss Enums with Mathematical Operations in C#.

SUMMARY:

In this article, I try to explain Understanding Enums in C# with examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *