I thought it was time to update the previous answers implementation to .Net4.0+ where generics are no longer needed thanks to contravariance on the IEqualityComparer<in T>
interface:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public sealed class ReferenceEqualityComparer
: IEqualityComparer, IEqualityComparer<object>
{
private ReferenceEqualityComparer() { }
public static readonly ReferenceEqualityComparer Default
= new ReferenceEqualityComparer();
public /*new*/ bool Equals(object x, object y)
{
return x == y; // This is reference equality! (See explanation below)
}
public int GetHashCode(object obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
Now there only needs to exist one instance for all your reference-equality checking instead of one for each type T
as was the case before.
You no longer have to specify T
every time you want to use this and also avoid polluting with unnecessary generic runtime types.
As for why x == y
is reference equality, it is because the ==
operator is a static method, which means it is resolved at compile-time, and at compile-time the x
and y
arguments are of type object
.
In fact this is what the Object.ReferenceEquals(object, object)
method source code looks like:
public static bool ReferenceEquals(object objA, object objB) {
return objA == objB;
}
To clarify for those who are not familiar with the concepts of Covariance and Contravariance...
class MyClass
{
ISet<MyClass> setOfMyClass = new HashSet<MyClass>(ReferenceEqualityComparer.Default);
}
...the above code compiles; Notice that it does not say HashSet<object>
.