Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability

I find my self overriding Equals() and GetHashCode() frequently to implement the semantic that business objects with identical property values are equal. That leads to code that is repetitive to write and fragile to maintain (property gets added and one/both of the overrides are not updated).

The code ends up looking something like this (comments on the implementation are welcome):

public override bool Equals(object obj)
{
    if (object.ReferenceEquals(this, obj)) return true;

    MyDerived other = obj as MyDerived;

    if (other == null) return false;

    bool baseEquals = base.Equals((MyBase)other);
    return (baseEquals && 
        this.MyIntProp == other.MyIntProp && 
        this.MyStringProp == other.MyStringProp && 
        this.MyCollectionProp.IsEquivalentTo(other.MyCollectionProp) && // See http://stackoverflow.com/a/9658866/141172
        this.MyContainedClass.Equals(other.MyContainedClass));
}

public override int GetHashCode()
{
    int hashOfMyCollectionProp = 0;
    // http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/
    // BUT... is it worth the extra math given that elem.GetHashCode() should be well-distributed?
    int bitSpreader = 31; 
    foreach (var elem in MyCollectionProp)
    {
        hashOfMyCollectionProp = spreader * elem.GetHashCode();
        bitSpreader *= 31;
    }
    return base.GetHashCode() ^ // ^ is a good combiner IF the combined values are well distributed
        MyIntProp.GetHashCode() ^ 
        (MyStringProp == null ? 0 : MyStringProp.GetHashValue()) ^
        (MyContainedClass == null ? 0 : MyContainedClass.GetHashValue()) ^
        hashOfMyCollectionProp;
}

My Questions

  1. Is the implementation pattern sound?
  2. Is ^ adequate given that the contributing component values are well-distributed? Do I need to multiply by 31-to-the-N when combining collection elements given their hash is well distributed?
  3. It seems this code could be abstracted into code that uses reflection to determine public properties, builds an expression tree that matches the hand-coded solution, and executes the expression tree as needed. Does that approach seem reasonable? Is there an existing implementation somewhere?
like image 571
Eric J. Avatar asked Mar 14 '12 18:03

Eric J.


2 Answers

MSDN actually doesn't say "don't overload Equals et al for mutable types". It used to say that, but now it says:

When you define a class or struct, you decide whether it makes sense to create a custom definition of value equality (or equivalence) for the type. Typically, you implement value equality when objects of the type are expected to be added to a collection of some sort, or when their primary purpose is to store a set of fields or properties.

http://msdn.microsoft.com/en-us/library/dd183755.aspx

Still, there are complexities surrounding stability of the hash code while an object participates in a hashed collection (Dictionary<T,U>, HashSet<T>, etc.).

I decided to opt for the best of both worlds, as outlined here:

https://stackoverflow.com/a/9752155/141172

like image 129
Eric J. Avatar answered Sep 20 '22 03:09

Eric J.


I find my self overriding Equals() and GetHashCode() frequently

  • MSDN says : don't overload Equals et al for mutable types

Is ^ adequate given that the contributing component values are well-distributed?

  • Yes, but hey are not always well distributed. Consider int properties. Shifting with some (small) prime numbers is advised.
like image 24
Henk Holterman Avatar answered Sep 20 '22 03:09

Henk Holterman