Check if two enums are equal or not in C#

Enum.Equals(Object) Method is used to check whether the current instance is equal to a specified object or not. This method overrides ValueType.Equals(Object) to define how enumeration members are evaluated for equality.

Syntax:

public override bool Equals (object obj);

Here, obj is an object to compare with the current instance, or null.

Returns: This method returns true if obj is an enumeration value of the same type and with the same underlying value as current instance otherwise, false.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Enum.Equals(Object) Method
using System;
  
class GFG {
  
    // taking two enums
    enum Clothes { Jeans,
                   Shirt }
    ;
    enum Colors { Blue,
                  Black }
    ;
  
    // Main Method
    public static void Main()
    {
  
        Clothes cl1 = Clothes.Jeans;
        Clothes cl2 = Clothes.Shirt;
  
        Colors c1 = Colors.Blue;
        Colors c2 = Colors.Black;
        Colors c3 = Colors.Blue;
  
        // using the method
        Console.WriteLine(c1.Equals(c3));
        Console.WriteLine(c1.Equals(c2));
        Console.WriteLine(cl1.Equals(cl2));
    }
}

chevron_right


Output:

True
False
False

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.