Home  >  Article  >  Backend Development  >  Check if two enumerations are equal in C#

Check if two enumerations are equal in C#

WBOY
WBOYforward
2023-09-03 14:33:02941browse

C# 中检查两个枚举是否相等

Enumeration, short for enumeration, is a fundamental part of the C# programming language. They allow developers to define a variable type that can have one of several predefined constants. Knowing how to compare two enumerations for equality can be an important tool in your C# programming toolbox. This article will guide you through the entire process, and by the end, you will be proficient at comparing two enumeration values ​​in C#.

Understanding enumerations in C

#Before we continue, it is necessary to understand what an enumeration is. An enumeration is a value type in C# used to represent a collection of named constants, often called an enumerator list.

This is a simple example of an enumeration −

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

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

Compare two enumerations

In C#, checking whether two enumeration values ​​are equal is very simple. You just use the == operator.

Example

This is 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. Then we use == operator to check if day1 and day2 are equal.

Output

The days are equal.

Compare enums with different cases

C# is case sensitive, which means Days.Monday and Days.monday will be considered different. However, you may encounter a situation where you want to compare two enumeration values ​​that have the same spelling but different case.

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

Example

This is 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 value to a string. We then use the String.Equals method and StringComparison.OrdinalIgnoreCase to compare strings regardless of case.

Output

The days are equal.

in conclusion

In C#, comparing two enumeration values ​​is very simple and straightforward. You can easily check whether two enumeration values ​​are equal by using the == operator or the String.Equals method for case-insensitive comparison.

The above is the detailed content of Check if two enumerations are equal in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete