C# Enum Equals Method

The Equals() method in C# is used to compare enum values for equality. It returns true if both enum values have the same underlying value, and false otherwise.

Syntax

Following is the syntax for using the Equals() method with enums −

enumValue1.Equals(enumValue2)

Parameters

  • enumValue2 − The enum value to compare with the current enum value

Return Value

The method returns a bool value −

  • true if both enum values are equal

  • false if the enum values are different

Enum Equals Comparison HardDrive Value: 0 prod1 HardDrive Value: 0 prod2 prod1.Equals(prod2) true

Using Equals() with Same Enum Values

When comparing enum values from the same enum type with identical values, Equals() returns true

using System;

class Program {
   enum Products { HardDrive, PenDrive, Keyboard };
   
   static void Main() {
      Products prod1 = Products.HardDrive;
      Products prod2 = Products.HardDrive;
      
      Console.WriteLine("prod1 = " + prod1);
      Console.WriteLine("prod2 = " + prod2);
      Console.WriteLine("prod1.Equals(prod2) = " + prod1.Equals(prod2));
   }
}

The output of the above code is −

prod1 = HardDrive
prod2 = HardDrive
prod1.Equals(prod2) = True

Using Equals() with Different Enum Values

When comparing enum values that have different underlying values, Equals() returns false

using System;

class Program {
   enum Products { HardDrive, PenDrive, Keyboard };
   
   static void Main() {
      Products prod1 = Products.HardDrive;
      Products prod2 = Products.PenDrive;
      
      Console.WriteLine("prod1 = " + prod1);
      Console.WriteLine("prod2 = " + prod2);
      Console.WriteLine("prod1.Equals(prod2) = " + prod1.Equals(prod2));
      Console.WriteLine("Underlying values: " + (int)prod1 + " vs " + (int)prod2);
   }
}

The output of the above code is −

prod1 = HardDrive
prod2 = PenDrive
prod1.Equals(prod2) = False
Underlying values: 0 vs 1

Comparing Different Enum Types

The Equals() method can also compare enum values from different enum types. It returns false even if the underlying values are the same −

using System;

class Program {
   enum Products { HardDrive, PenDrive, Keyboard };
   enum Categories { Hardware, Software, Accessories };
   
   static void Main() {
      Products prod = Products.HardDrive;
      Categories cat = Categories.Hardware;
      
      Console.WriteLine("prod = " + prod + " (value: " + (int)prod + ")");
      Console.WriteLine("cat = " + cat + " (value: " + (int)cat + ")");
      Console.WriteLine("prod.Equals(cat) = " + prod.Equals(cat));
      Console.WriteLine("Different enum types, even with same underlying value");
   }
}

The output of the above code is −

prod = HardDrive (value: 0)
cat = Hardware (value: 0)
prod.Equals(cat) = False
Different enum types, even with same underlying value

Conclusion

The Equals() method for enums compares both the underlying values and the enum types. It returns true only when both enum values belong to the same enum type and have identical underlying values, making it a reliable way to check enum equality in C#.

Updated on: 2026-03-17T07:04:35+05:30

800 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements