This interface is not implemented by the object itself but by an external class
This is an interface for an object that performs the comparison of two objects
If there are multiple ways to compare two objects then this is the right choice
When testing two instances for equality you have to make an explicit choice of which IEqualityComparer instance to use.

Which Interface

IEquatable - Provides an equality check when there is only one way of comparing the objects (implemented inside the class)
IEqualityComparer - Allows you to define and use multiple equality checks (implemented outside the class)

Defines methods to support the comparison of objects of equality
Do not derive from the interface directly but from the EqualityComparer class instead

public class MyClass : System.Collections.Generic.EqualityComparer < MyObject > 
   public bool Equals(MyObject x, MyObject y)
      return x.Equals(y);
   public int GetHashCode(MyObject obj)
      return obj.GetHashCode();

Equals Method

GetHashCode Method

If your class overrides the Equals method and does value equality then you should override this method
Returns a 32-bit integer representation of the object.

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext