0

Which method GetHashCode() is to prefer?

  • Concating all properties and GetHashCode() of the result

  • GetHashCode() for each property and adding each

Code:

public class Foo
{
    public int FooId { get; set; }
    public string FooName { get; set; }
    public List<string> FooList  { get; set; }

    public override int GetHashCode() // 1st way
    {
        return FooId.GetHashCode() + FooName.GetHashCode() + FooList.GetHashCode() ;
    }


    public override int GetHashCode() // 2nd way
    {
        return  string.Concat(FooId, FooName, string.Concat(FooList)).GetHashCode();
    }
}
Toshi
  • 2,532
  • 4
  • 17
  • 45
  • Do you really need to compare all properties in the hashcode, when you have an Id field? Isn't that unique? – ChoockY Jun 27 '17 at 13:45
  • 3
    Neither is a good solution. If you do a search for how to combine hash codes, you'll find numerous far superior options. – Servy Jun 27 '17 at 13:46
  • @ChoockY no I need to check if the object was modified (same ID) by comparing hashcodes instead of all properties – Toshi Jun 27 '17 at 13:48
  • 2
    *"comparing hashcodes instead of all properties"* -> Be aware that hashcodes are not guaranteed to be different for different values! Hashcodes are only guaranteed to be the same for equal values, but 2 different values may still produce the same hashcode. So you should not rely only on hashcodes for equality comparison. – bassfader Jun 27 '17 at 13:53
  • _Hashcodes are only guaranteed to be the same for equal values_ sure? – Toshi Jun 27 '17 at 13:54
  • 2
    You should not use hash codes for equality testing, since two different values can hash to the same value. –  Jun 27 '17 at 13:57
  • Comparing only the hashcodes isn't enough. The hashcode is used to find a "bucket" in which an element is expected to exist. Elements in different buckets cannot be the same, but elements in the same bucket can be different. In this cases the Equals method determines the equality. I think you should just check the Id in hashcode and check all properties in the equals (AND relation). – ChoockY Jun 27 '17 at 13:58
  • @Toshi Absolutely sure, the MSDN documentation of [String.GetHashCode()](https://msdn.microsoft.com/en-us/library/system.string.gethashcode(v=vs.110).aspx) for example says the following: *"If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code."* – bassfader Jun 27 '17 at 13:59
  • @bassfader [Object.GetHashCode()](https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx) has even better documentation in its notes to inheritors section *"If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two objects do not have to return different values."* – Scott Chamberlain Jun 27 '17 at 14:46

0 Answers0