Is there a default IEqualityComparer<T>
implementation that uses ReferenceEquals
?
EqualityComparer<T>.Default
uses ObjectComparer, which uses object.Equals()
. In my case, the objects already implement IEquatable<T>
, which I need to ignore and compare by object's reference only.
Just in case there is no default implementation, this is my own:
Edit by 280Z28: Rationale for using
RuntimeHelpers.GetHashCode(object)
, which many of you probably haven't seen before. :) This method has two effects that make it the correct call for this implementation:ReferenceEquals
works for null parameters, so should the comparer's implementation of GetHashCode().Object.GetHashCode()
non-virtually.ReferenceEquals
specifically ignores any overrides ofEquals
, so the implementation of GetHashCode() should use a special method that matches the effect of ReferenceEquals, which is exactly what RuntimeHelpers.GetHashCode is for.[end 280Z28]