简体   繁体   中英

static Object.Equals method, default implementation of GetHashCode and the Dictionary class

I just want to confirm my understanding of a few fundamentals. Hope you don't mind!

I understand the static equals method

Object.Equals(objA, objB)

first checks for reference equality. If not equal by reference, then calls the object instance equals method

objA.Equals(objB)

Currently in my override for equals, i first check for reference equality, and if not equal referentially then check with all members to see if the semantics are the same. Is this a good approach? If so, then the static version seems superfluous?

Also what exactly does the default GetHashCode for an object do?

If I add my object to a dictionary which is a HashTable underneath and don't override equals and GetHashCode, then I guess I should do to make it sort optimally hence better retrieval time?

Currently in my override for equals, i first check for reference equality, and if not equal referentially then check with all members to see if the semantics are the same. Is this a good approach? If so, then the static version seems superfluous?

Yes, it's a great idea to do the fast reference-equality check. There's no guarantee that your method will be called through the static Object.Equals method - it could well be called directly. For example, EqualityComparer<T>.Default (the typical middleman for equality checking) will directly call this method in many situations (when the type does not implement IEquatable<T> ) without first doing a reference-equality check.

Also what exactly does the default GetHashCode for an object do?

It forwards to RuntimeHelpers.GetHashCode : a magic, internally-implemented CLR method that is a compliant GetHashCode implementation for reference-equality. For more information, see Default implementation for Object.GetHashCode() . You should definitely override it whenever you override Equals .

EDIT:

If I add my object to a dictionary which is a HashTable underneath and don't override equals and GetHashCode, then I guess I should do to make it sort optimally hence better retrieval time?

If you don't override either, you'll get reference-equality with (probably) a well-balanced table. If you override one but not the other or implement them in any other non-compliant way, you'll get a broken hashtable.

By the way, hashing is quite different from sorting.

For more information, see Why is it important to override GetHashCode when Equals method is overriden in C#?

Your first question was already answered, but I think the second was not fully answered.

Implementing your GetHashCode is important if you want to use your object as a key in a hash table or a dictionary. It minimizes collisions and therefore it speeds the lookup. A lookup collision happens when two or more keys have the same hashcode and for those equals method is invoked. If the hashcode is unique, an equals will only be called once, otherwise it will be called for every key with the same hashcode until the equals returns true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM