Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add proper reference-comparison semantics to CustomEqualityComparer

  • Loading branch information...
commit e2576b6f948e2780d643929310011eedc4f9f39f 1 parent 495711c
@cdhowie authored
Showing with 21 additions and 0 deletions.
  1. +21 −0 Cdh.Toolkit.Collections/CustomEqualityComparer.cs
View
21 Cdh.Toolkit.Collections/CustomEqualityComparer.cs
@@ -47,11 +47,32 @@ public CustomEqualityComparer(Func<TObject, TKey> keySelector)
public bool Equals(TObject x, TObject y)
{
+ if (!typeof(TObject).IsValueType) {
+ // Reference semantics.
+ if (object.ReferenceEquals(x, y)) {
+ return true;
+ }
+
+ if (object.ReferenceEquals(x, default(TObject))) {
+ return object.ReferenceEquals(y, default(TObject));
+ }
+
+ if (object.ReferenceEquals(y, default(TObject))) {
+ return false;
+ }
+ }
+
return EqualityComparer<TKey>.Default.Equals(keySelector(x), keySelector(y));
}
public int GetHashCode(TObject obj)
{
+ if (!typeof(TObject).IsValueType) {
+ if (object.ReferenceEquals(obj, default(TObject))) {
+ throw new ArgumentNullException("obj");
+ }
+ }
+
return EqualityComparer<TKey>.Default.GetHashCode(keySelector(obj));
}
Please sign in to comment.
Something went wrong with that request. Please try again.