Check if two enumerations are equal in C#

WBOY
Release: 2023-09-03 14:33:02
forward
1000 people have browsed it

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
}
Copy after login

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.");
      }
   }
}
Copy after login

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.
Copy after login
Copy after login

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.");
      }
   }
}
Copy after login

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.
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template