System.Collections.Generic.IEqualityComparer
Added in .NET 2.0
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.
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();
}
}
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.
.NET 1.1
It was possible to compare for equality by overriding the virtual object Equals method
public override bool Equals(object obj)
{
var other obj As MyObject
if (other == null) { return false; }
//etc
}
This method is loosely typed because it takes an object as the parameter
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext