Advertisement
he-dev

Untitled

Oct 17th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. void Main()
  2. {
  3.     (new Person() == null).Dump();
  4. }
  5.  
  6. class Person : IEquatable<Person>
  7. {
  8.     public static readonly IEqualityComparer<Person> Comparer = new PersonEqualityComparer();
  9.    
  10.     public bool Equals(Person other)
  11.     {
  12.         return Comparer.Equals(this, other);
  13.     }
  14.  
  15.     public static bool operator ==(Person x, Person y) => x?.Equals(y) == true;
  16.    
  17.     public static bool operator !=(Person x, Person y) => !(x == y);
  18. }
  19.  
  20. class PersonEqualityComparer : IEqualityComparer<Person>
  21. {
  22.     public bool Equals(Person x, Person y)
  23.     {      
  24.         if(x == null) return true; // Bum!
  25.         //if (ReferenceEquals(x, null)) return true; // No bum!
  26.        
  27.         return false;
  28.     }
  29.  
  30.     public int GetHashCode(Person obj)
  31.     {
  32.         return 0;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement