What is the best way to implement this composite GetHashCode()
- by Frank Krueger
I have a simple class:
public class TileName {
int Zoom, X, Y;
public override bool Equals (object obj)
{
var o = obj as TileName;
return (o != null) && (o.Zoom == Zoom) && (o.X == X) && (o.Y == Y);
}
public override int GetHashCode ()
{
return (Zoom + X + Y).GetHashCode();
}
}
I was curious if I would get a better distribution of hash codes if I instead did something like:
public override int GetHashCode ()
{
return Zoom.GetHashCode() + X.GetHashCode() + Y.GetHashCode();
}
This class is going to be used as a Dictionary key, so I do want to make sure there is a decent distribution.