If the type has not overridden the Equals
or GetHashCode
methods then their default implementations, from object
, do what you want, namely provide equality based on their identity, rather than their value. You can use EqualityComparer<Person>.Default
to get an IEqualityComparer
that uses those semantics if you want.
If the Equals
method has been overridden to provide some sort of value semantics, but you don't want that, you want identity semantics, then you can use object.ReferenceEquals
in your own implementation:
public class IdentityComparer<T> : IEqualityComparer<T>
{
public bool Equals(T x, T y)
{
return object.ReferenceEquals(x, y);
}
public int GetHashCode(T obj)
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
}
}