How To Compare Enum Values In C#

The existing instance can be compared to a specified object by utilizing the Enum.CompareTo(Object) method, offering a relative value representation.

Syntax:

It has the following syntax:

Example

public int CompareTo(object target);
  • 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

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

Let's consider an instance to showcase how to compare enum constants in C#.

Example

using System; 
public class Demo 
{ 
	enum Days
	{ 
		Monday, Wednesday, Friday 
	}; 
	public static void Main(String[] args) 
	{ 
		Days day_1 = Days.Monday; 
	    Days day_2 = Days.Wednesday; 
		Days day_3 = Days.Monday; 
		Days day_4 = Days.Friday; 
		Console.Write("Comparing {0} with {1}: ", day_1, day_2); 
		Console.WriteLine(day_1.CompareTo(day_2)); 
		Console.Write("Comparing {0} with {1}: ", day_1, day_3); 
		Console.WriteLine(day_1.CompareTo(day_3)); 
		Console.Write("Comparing {0} with {1}: ", day_4, day_2); 
		Console.WriteLine(day_4.CompareTo(day_2)); 
	} 
}

Output:

Output

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

Explanation:

  • In this instance, the Days enum's enumeration values can be compared by utilizing the CompareTo function, as demonstrated in the following C# code snippet.

Inside the main method:

  • Specific enum values corresponding to Monday, Wednesday, and Friday are defined and initialized for the enum values day1, day2, day3, and day4.
  • 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 day1 and day3 represent Monday, the second comparison (day1 vs. day3) returns a zero (0) result.
  • As Friday (day 4) appears after Wednesday (day 2) in the enum specification, the third comparison (day4 vs. day2) returns a positive result (1).
  • Example-2:

Let's consider another example to illustrate the process of comparing enum values in C#.

Example

using System;
//Defining an enum
public enum DayofWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}
class Demo
{
    static void Main(string[] args)
    {
        // Example 1: Using Equality Comparison
        DayofWeek today = DayofWeek.Friday;
        DayofWeek tomorrow = DayofWeek.Wednesday;
        if (today == tomorrow)
        {
            Console.WriteLine("Today and Tomorrow both are the same day.");
        }
        else
        {
            Console.WriteLine("Today and Tomorrow are both different days.");
        }
        // Example 2: Using Comparison Methods
        DayofWeek day_1 = DayofWeek.Wednesday;
        DayofWeek day_2 = DayofWeek.Saturday;
        if (Enum.Equals(day_1, day_2))
        {
            Console.WriteLine("Day1 and Day2 both are equal.");
        }
        else
        {
            Console.WriteLine("Day1 and Day2 are both not equal.");
        }
        // Example 3: Using Switch Statement
        DayofWeek chosenDay = DayofWeek.Friday;
        switch (chosenDay)
        {
            case DayofWeek.Saturday:
            case DayofWeek.Sunday:
                Console.WriteLine("It's a wonderful weekend!");
                break;
            default:
                Console.WriteLine("It's a boring weekday.");
                break;
        }
    }
}

Output:

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:

  • 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.
  • Comparison Methods: The Equals method compares two enum values (day1 and day2). It determines whether or not they are equal and prints the result accordingly.
  • 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 serves as a general illustration demonstrating the process of comparing enum values in C#. It illustrates various methods of handling enum values based on different scenarios, such as utilizing equality comparison, comparison methods, and switch statements.

Input Required

This code uses input(). Please provide values below: