C# GetHashCode with three Int16 values?
I'm using this function for a key in C#'s hash map like class, "Dictionary".
x, y and z are Int16.
public override int GetHashCode()
{
return (x << 16) | (UInt16)y;
}
How could I extend this to using all 3 variables?
See What is the best algorithm for an overridden System.Object.GetHashCode? for the even more general case with any number of variables, of any type.
For three variables x, y, z of any type, the standard method is as follows:
return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
^ is the XOR operator.
You can incorporate additional variables into your method using the XOR operator as well.
链接地址: http://www.djcxy.com/p/39770.html