I have a structure in C#:
public struct UserInfo
{
public string str1
{
get;
set;
}
public string str2
{
get;
set;
}
}
The only rule is that UserInfo(str1="AA", str2="BB").Equals(UserInfo(str1="BB", str2="AA"))
How to override the GetHashCode function for this structure?
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