0

I over-rid the Equals method and equality operators and Resharper was throwing a warning saying I should also override GetHashCode. So I did. I just did this:

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

But now it's giving me another warning saying GetHashCode calls base.GetHashCode. So, is what I'm going bad? Why is Resharper giving this warning? Should I create my own implementation of GetHashCode? In fact, I'm thinking why I even bothered to override it.

  • it just adds complexity. – Daniel A. White Jun 08 '15 at 00:00
  • 3
    if you don't know what you are doing return 0; otherwise you will run into hard bugs when equal objects get put into different buckets. – MrDosu Jun 08 '15 at 00:09
  • 2
    @MrDosu: Not the best advice. Then you're trying to figure out why your performance is tanking, and guess what? You are forced to understand what you are doing anyways. Advice: make sure you understand what you are doing at all times. Don't code by accident. – sstan Jun 08 '15 at 00:42
  • Highly suggested reading: http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden – sstan Jun 08 '15 at 00:46
  • @sstan I disagree, in 90% of all situations actually implementing the GetHashCode is wasting money and if you are using a HashSet for performance you know what a hashcode is. Implementing something because "We might eventually use it in the future" is terrible practice. Also writing a good hash function is more difficult then it appears. – MrDosu Jun 08 '15 at 00:48
  • @MrDosu, I find it plausible that someone would use a class as a Dictionary key for associativity rather than in a HashSet for performance. Clearly, writing a good hash function is hard, but then again it's not too hard to beat `return 0`. – zneak Jun 08 '15 at 01:07
  • @MrDosu: There are actually a bunch of developers out there that use HashSet or Dictionary without understanding what a hashcode is. However, to your point of implementing something that is not currently needed, I do agree there. But I would then throw an exception rather than return 0. That would make it very obvious to anyone attempting to use the object in a HashSet, that they need to have a look at the `GetHashCode` implementation. Otherwise, you run the risk of forgetting about it and finding out about it when it's too late. – sstan Jun 08 '15 at 01:08
  • That's the question. I'm just asking if there's anything wrong with calling 'base.GetHashCode'. –  Jun 09 '15 at 03:47

2 Answers2

3

Two equal objects should have the same hash code. Otherwise, the hash code is meaningless and worthless. This is why Resharper warns you. (The opposite relation is not true, as different objects may have different hash codes.)

The base implementation returns a value based on the object's handle, and very possibly returns a different value for equal (by your Equals definition) objects. It's not an issue if you don't use it, but Dictionary and HashSet do. They will break badly if you don't correctly implement GetHashCode.

zneak
  • 134,922
  • 42
  • 253
  • 328
0

It is necessary to override GetHashCode if the item will be used as a key in a Dictionary, HashSet, etc, since this is used to group items into buckets. Otherwise, you can avoid doing this.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125