3

Possible Duplicate:
Why is it important to override GetHashCode when Equals method is overriden in C#?

I dont implement the GetHashCode method of the Object class. so I get a number of warnings.

Is there a way to check for equality where I just check the hash code in the Equals method, so implement both Equals and GetHashCode and not get the "Object.GetHashCode not implemented warning?".

what will happen if i just implement the Equals and not implement the GetHashCode? instances of myclass are updatable in my application.

public class MyClass{

private string x;
private string y;

public override bool Equals(object obj)
        {

            try
            {

                 return    Object.Equals(this.x, obj.x)
                        && Object.Equals(this.y, obj.y);

            }
            catch(Exception Ex)
            {
                log.Debug(Ex.StackTrace);
                throw; 
            }
        }

}
Community
  • 1
  • 1
Utpal Mattoo
  • 890
  • 3
  • 17
  • 41
  • 2
    Does http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c/371348#371348 answer most? all? of this? Possible duplicate? – Marc Gravell May 17 '12 at 21:29
  • 1
    btw, that `Equals` implementation is massively overkill; that only needs to be `return this.x == other.x && this.y == other.y;` – Marc Gravell May 17 '12 at 21:31
  • I come from the Java world. In Java i compared references with ==. Although string are reference types in c#, they can be used like value types (using the == operator), unlike in Java where i would compare value equality with str1.Equals(str2). Specifically in this case, if i were to use this.str1 == this.str2 - and str1 were null, I think i will get a NULL pointer exception? – Utpal Mattoo May 17 '12 at 22:46

3 Answers3

3

If you implement Equals but not GetHashCode, then if your class is used in any data structure which uses hashing (HashSet<T>, Dictionary<K,V>, etc.) it will behave incorrectly. Even if you don't explicitly use those data structures, it's possible that they're used by some code (i.e., some Linq operations use HashSets), so it's safer to just override GetHashCode as well.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • In particular Linq-methods that use an instance of `IEqualityComparer` need a correct hashcode to work. As far as I know this are `Except`, `Distinct`, `Union` and `Intersect`. – MakePeaceGreatAgain Jun 08 '17 at 14:22
2

GetHashCode is used for storing and retrieving objects in a has table, i.e., it is another way to judge object equality.

If you override one (Equals) and not the other (GetHashCode) then you run the risk of encountering inconsistent behavior when using GetHashCode (i.e., plopping your objects in a collection implemented as a hash table) versus Equals().

You may enjoy reading this post by Eric Lippert on the subject.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

If you don't implement GetHashCode, your class will behave erratically when used as a key in structures like dictionaries or hash sets. If you want to use those types of structures, there's no way around it. Technically, your application will work if you stay away from those, but it's bad form, and anyone else who uses your classes will be in for a rude surprise.

recursive
  • 83,943
  • 34
  • 151
  • 241