What is the best way to implement this composite GetHashCode()

Posted by Frank Krueger on Stack Overflow See other posts from Stack Overflow or by Frank Krueger
Published on 2010-04-28T22:29:44Z Indexed on 2010/04/28 22:37 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about hashing

Related posts about c#