6

I have a class that looks like this.

public class Point : IEquatable<Point>
{
    public int _xValue {get;set;}
    public double _yValue {get;set;}

    public Point(int x, double y)
    {
        _xValue = x;
        _yValue = y;
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as Point);
    }

    public bool Equals(Point obj)
    {
        return obj != null && obj._xValue == this._xValue && obj._yValue == this._yValue;
    }


}

I have to implement the GetHashCode function so that I can use it as a dictionary key. But I'm not clear on what the GetHashCode function has to return in this situation. Can someone help me out?

Aks
  • 5,188
  • 12
  • 60
  • 101
  • Could this be what you are looking for? http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode – Adriaan Stander Feb 03 '11 at 05:33
  • @astander: When a question has already been asked and answered before, mark it as a dupe. You've got plenty of rep for that. (Or are you out of votes for today?) – Ben Voigt Feb 03 '11 at 05:51
  • I was not sure if this was exactly what the OP was after. That is why I did not just close it. – Adriaan Stander Feb 03 '11 at 05:52

5 Answers5

4

It should return an integer, preferably unique for each seperate instance of the object. A hash value is basically a single number created out of the contents of an object, used to uniquely identify that object. The number one rule is that if two of those Points evaluate as equal to each other, the hash value should be the same for both of them.

A more detailed description is available at MSDN

Xono
  • 1,900
  • 17
  • 20
2

The GetHashCode function needs to return am integer that will uniquely identify one instance of the object from another so as to avoid collisions when it is used as a key in the dictionary.

You should be able to reliably reproduce the hash code so try and avoid using random or date values as seeds for the hash code.

lomaxx
  • 113,627
  • 57
  • 144
  • 179
1

You could do _xValue ^ _yValue.GetHashCode()

leeny
  • 626
  • 3
  • 14
1

There are some of basic rules for this:

  • Bool: if true return 0, otherwise return 1
  • Byte, char, short or int: return the value of the type
  • Long: return (int)(f ^ f(>>>32))
  • Float: return Convert.ToInt32 of the value of the type
  • Object: Return the value generated by calling object.GetHashCode()
  • Array: Iterate all the array and treat each element individually

so in your case you have x = int and y = double, if you follow this rules, you'll find the solution.

return x ^ y.GetHashCode();

You can always add fancy mathematical algorithms but be careful with numeric collisions.

Fer Osorio
  • 31
  • 1
  • 6
  • converting to int would violate uniqueness (2.6 -> 3 and so does 2.9 etc). i think it makes more sense to do x ^ y.GetHashCode() – leeny Feb 04 '11 at 04:56
0

In your case, you could do something like:

return x ^ y;

logicnp
  • 5,796
  • 1
  • 28
  • 32