Javatpoint Logo
Javatpoint Logo

How to compare Enum values in C#?

The current instance can be compared to a given object using the Enum.CompareTo(Object) method, which provides a relative value indication.

Syntax:

It has the following syntax:

  • public: It indicates the method's level of access. It indicates that the method may be accessed from outside the class.
  • int: It denotes that an integer value is returned by the method.
  • CompareTo: The name of the method.
  • (object target): It indicates the method's parameter. It takes an object (target) as input to compare an object with the current object.

Return Value

  • Negative integer: The current object is less than the target object.
  • Zero: The current object is equal to the target object.
  • Positive integer: The current object is greater than the target object.

Exceptions

  1. ArgumentException: It will occur when the type of the target and the current instance differ.
  2. InvalidOperationException: If the instance is not of type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64, an invalid operation exception will occur.
  3. NullReferenceException: If the instance is null, NullReferenceException is raised.

Example-1:

Let us take an example to demonstrate how to compare enum values in C#.

Output:

Comparing Monday with Wednesday: -1
Comparing Monday with Monday: 0
Comparing Friday with Wednesday: 1

Explanation:

  • In this example, the Days enum's enum values may be compared using the CompareTo method, which is shown in this C# code.

Inside the main method:

  • Specific enum values corresponding to Monday, Wednesday, and Friday are defined and initialized for the enum values day_1, day_2, day_3, and day_4.
  • Day 1, Day 2, and Day 4 are compared with other enum values (day 2, Day 3, and Day 2) using the CompareTo method.
  • The relative order of the enum values is displayed in the comparison results, which are printed to the console using WriteLine.
  • For example, the enum definition puts Monday (day 1) before Wednesday (day 2). Therefore, the initial comparison (day 1 vs. day 2) has a negative number (-1).
  • As day_1 and day_3 represent Monday, the second comparison (day_1 vs. day_3) returns a zero (0) result.
  • As Friday (day 4) appears after Wednesday (day 2) in the enum specification, the third comparison (day_4 vs. day_2) returns a positive result (1).

Example-2:

Let us take another example to demonstrate how to compare enum values in C#.

Output:

Today and Tomorrow are both different days.
Day 1 and Day 2 are both not equal.
It's a boring weekday.

Explanation:

This C# code demonstrates many methods for comparing enum values:

  1. Equality Comparison: The '==' operator compares two enum values (Today and Tomorrow). It prints the result based on whether the results are the same or different.
  2. Comparison Methods: The Equals() method compares two enum values (day_1 and day_2). It determines whether or not they are equal and prints the result accordingly.
  3. Switch Statement: A switch statement is used to determine the value of the chosenDay enum variable. It prints "It's a wonderful weekend!" if Saturday or Sunday is selected. If not, "It's a boring weekday" is printed.

This code acts as an overall example of how to compare enum values in C# and shows how to handle enum values depending on different situations by using equality comparison, comparison methods, and switch statements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news