Equality Implementation in C#

The equality pattern I like to see in C# is as follows:

For entity type classes

“Many objects are not fundamentally defined by their attributes, but rather by a thread of continuity and identity.” -Eric Evans

  1. /// <summary>  
  2. /// Entity object where identity is based on identifier (e.g. Person)  
  3. /// </summary>  
  4. class Entity : IEquatable<Entity>  
  5. {  
  6.     private readonly Int32 _Identifier;  
  7.            
  8.     public Entity(Int32 identifier)  
  9.     {  
  10.         _Identifier = identifier;  
  11.     }  
  12.    
  13.     public override bool Equals(object obj)  
  14.     {  
  15.         var result = Equals(obj as Entity);  
  16.         return result;  
  17.     }  
  18.    
  19.     public override int GetHashCode()  
  20.     {  
  21.         var result = _Identifier.GetHashCode();  
  22.         return result;  
  23.     }  
  24.    
  25.    
  26.     public bool Equals(Entity other)  
  27.     {  
  28.         if (ReferenceEquals(other, null))  
  29.             return false;  
  30.    
  31.         var result = _Identifier == other._Identifier;  
  32.         return result;  
  33.     }  
  34.    
  35.     // optional operators  
  36.     public static Boolean operator ==(Entity target, Entity other)  
  37.     {  
  38.         if (ReferenceEquals(target, other))  
  39.             return true;  
  40.    
  41.         if (ReferenceEquals(target, null))  
  42.             return false;  
  43.    
  44.         var result = target.Equals(other);  
  45.         return result;  
  46.     }  
  47.    
  48.     public static Boolean operator !=(Entity target, Entity other)  
  49.     {  
  50.         var result = !(target == other);  
  51.         return result;  
  52.     }  
  53.    
  54. }  

For value type classes

“Many objects have no conceptual identity. These objects describe characteristics of a thing.” -Eric Evans

  1. /// <summary>  
  2. /// Value object where identity is based on state (e.g. Money)  
  3. /// </summary>  
  4. class Value :IEquatable<Value>  
  5. {  
  6.     private readonly Int32 _Property1, _Property2;  
  7.    
  8.     public Value(Int32 property1, Int32 property2)  
  9.     {  
  10.         _Property1 = property1;  
  11.         _Property2 = property2;  
  12.     }  
  13.    
  14.     public override bool Equals(object obj)  
  15.     {  
  16.         var result = Equals(obj as Value);  
  17.         return result;  
  18.     }  
  19.    
  20.     public bool Equals(Value other)  
  21.     {  
  22.         if (ReferenceEquals(other, null))  
  23.             return false;  
  24.    
  25.         var result =  
  26.             _Property1 == other._Property1 &&  
  27.             _Property2 == other._Property2;  
  28.    
  29.         return result;  
  30.     }  
  31.    
  32.     public override int GetHashCode()  
  33.     {  
  34.         var result =  
  35.             _Property1.GetHashCode() ^  
  36.             _Property2.GetHashCode();  
  37.    
  38.         return result;  
  39.     }  
  40.    
  41.     // optional operators  
  42.     public static Boolean operator == (Value target, Value other)  
  43.     {  
  44.         if (ReferenceEquals(target, other))  
  45.             return true;  
  46.    
  47.         if (ReferenceEquals(target, null))  
  48.             return false;  
  49.    
  50.         var result = target.Equals(other);  
  51.         return result;  
  52.     }  
  53.    
  54.     public static Boolean operator !=(Value target, Value other)  
  55.     {  
  56.         var result = !(target == other);  
  57.         return result;  
  58.     }  
  59. }  

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles