How do you implement GetHashCode for structure with two string, when both strings are interchangeable
Solution 1:
MSDN:
A hash function must have the following properties:
- 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, theGetHashCode
methods for the two object do not have to return different values.- The
GetHashCode
method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object'sEquals
method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.- For the best performance, a hash function must generate a random distribution for all input.
Taking it into account correct way is:
return str1.GetHashCode() ^ str2.GetHashCode()
^
can be substituted with other commutative operation
Solution 2:
See Jon Skeet's answer - binary operations like ^
are not good, they will often generate colliding hash!
Solution 3:
public override int GetHashCode()
{
unchecked
{
return (str1 ?? String.Empty).GetHashCode() +
(str2 ?? String.Empty).GetHashCode();
}
}
Using the '+' operator might be better than using '^', because although you explicitly want ('AA', 'BB') and ('BB', 'AA') to explicitly be the same, you may not want ('AA', 'AA') and ('BB', 'BB') to be the same (or all equal pairs for that matter).
The 'as fast as possible' rule is not entirely adhered to in this solution because in the case of nulls this performs a 'GetHashCode()' on the empty string rather than immediately return a known constant, but even without explicitly measuring I am willing to hazard a guess that the difference wouldn't be big enough to worry about unless you expect a lot of nulls.
Solution 4:
As a general rule, a simple way to generate a hashcode for a class is to XOR all the data fields that can participate in generating the hash code (being careful to check for null as pointed out by others). This also meets the (artificial?) requirement that the hashcodes for UserInfo("AA", "BB") and UserInfo("BB", "AA") are the same.
If you can make assumptions about the use of your class, you can perhaps improve your hash function. For example, if it is common for str1 and str2 to be the same, XOR may not be a good choice. But if str1 and str2 represent, say, first and last name, XOR is probably a good choice.
Although this is clearly not meant to be a real-world example, it may be worth pointing out that: - This is probably a poor example of use of a struct: A struct should normally have value semantics, which doesn't seem to be the case here. - Using properties with setters to generate a hash code is also asking for trouble.
Solution 5:
Going along the lines ReSharper is suggesting:
public int GetHashCode()
{
unchecked
{
int hashCode;
// String properties
hashCode = (hashCode * 397) ^ (str1!= null ? str1.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (str2!= null ? str1.GetHashCode() : 0);
// int properties
hashCode = (hashCode * 397) ^ intProperty;
return hashCode;
}
}
397 is a prime of sufficient size to cause the result variable to overflow and mix the bits of the hash somewhat, providing a better distribution of hash codes. Otherwise there's nothing special in 397 that distinguishes it from other primes of the same magnitude.