Check if Two enums Are Equal or Not in C#


Enums, short for enumerations, are a fundamental part of the C# programming language. They allow developers to define a type of variable that can have one of a few predefined constants. Understanding how to compare two enums for equality can be a vital tool in your C# programming toolbox. This article will guide you through the process, and by the end, you will be proficient in comparing two enum values in C#.

Understanding Enums in C#

Before we move forward, it's essential to grasp what enums are. Enums are value types in C# and are used to represent a collection of named constants, usually called enumerator lists.

Here's a simple example of an enum −

public enum Days {
   Sunday,
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday
}

In this example, Days is an enum, and Sunday, Monday, etc., are its members.

Comparing Two Enums

Checking if two enum values are equal is straightforward in C#. You simply use the == operator.

Example

Here's an example −

using System;

public enum Days {
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday,
   Sunday
}

public class Program {
   public static void Main() {
      Days day1 = Days.Monday;
      Days day2 = Days.Monday;

      if (day1 == day2) {
         Console.WriteLine("The days are equal.");
      } else {
         Console.WriteLine("The days are not equal.");
      }
   }
}

In this code snippet, we first define two variables day1 and day2 of type Days. We then use the == operator to check if day1 and day2 are equal.

Output

The days are equal.

Comparing Enums with Different Cases

C# is case-sensitive, which means Days.Monday and Days.monday would be considered different. However, you may have a situation where you want to compare two enum values that are spelled the same but have different cases.

You can do this by converting the enum values to strings and then comparing the strings using the String.Equals method with StringComparison.OrdinalIgnoreCase as a parameter.

Example

Here's an example −

using System;

public enum Days {
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday,
   Sunday
}

public class Program {
   public static void Main() {
      string day1 = Days.Monday.ToString();
      string day2 = "monday";

      if (String.Equals(day1, day2, StringComparison.OrdinalIgnoreCase)) {
         Console.WriteLine("The days are equal.");
      } else {
         Console.WriteLine("The days are not equal.");
      }
   }
}

In this example, we first convert the enum values to strings. We then use the String.Equals method with StringComparison.OrdinalIgnoreCase to compare the strings without considering their case.

Output

The days are equal.

Conclusion

In C#, comparing two enum values is simple and straightforward. By using the == operator, or the String.Equals method for case-insensitive comparisons, you can easily check if two enum values are equal.

Updated on: 24-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements