Advertisement
GeneralGDA

Fed up with equality impl.

Jul 25th, 2017
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. public abstract class SingleValueEncapsulator<T>
  2. {
  3.     private readonly T _value;
  4.  
  5.     public SingleValueEncapsulator(T value)
  6.     {
  7.         _value = value;
  8.     }
  9.  
  10.     protected T Value
  11.     {
  12.         get { return _value; }
  13.     }
  14.  
  15.     protected bool Equals(SingleValueEncapsulator<T> other)
  16.     {
  17.         return EqualityComparer<T>.Default.Equals(_value, other._value);
  18.     }
  19.  
  20.     public override bool Equals(object candidate)
  21.     {
  22.         if (ReferenceEquals(null, candidate))
  23.         {
  24.             return false;
  25.         }
  26.  
  27.         if (ReferenceEquals(this, candidate))
  28.         {
  29.             return true;
  30.         }
  31.  
  32.         if (candidate.GetType() != this.GetType())
  33.         {
  34.             return false;
  35.         }
  36.  
  37.         return Equals((SingleValueEncapsulator<T>) candidate);
  38.     }
  39.  
  40.     public override int GetHashCode()
  41.     {
  42.         return EqualityComparer<T>.Default.GetHashCode(_value);
  43.     }
  44. }
  45.  
  46. class FileUid
  47. {
  48.     // ...
  49. }
  50.  
  51. class FileUidOperation : SingleValueEncapsulator<FileUid>
  52. {
  53.     public FileUidOperation() : base(new FileUid())
  54.     {
  55.     }
  56.  
  57.     public FileUid Target => Value;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement