Mike
Mike

Reputation: 6239

Good resource for explaining how hash codes are used in collections

Can anyone give a good explanation and / or links to a good resource of how hash codes are used in storing and retrieving objects in hashtables, dictionaries etc, specifically in C# / .NET.

I'm interested to see how Equals and GetHashCode are used collectively when storing and retrieving items.

Upvotes: 1

Views: 177

Answers (3)

codekaizen
codekaizen

Reputation: 27419

This is a pretty good demo: http://research.cs.vt.edu/AVresearch/hashing/buckethash.php

Upvotes: 1

James Westgate
James Westgate

Reputation: 11444

try object.GetHashCode.

"A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection. The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table."

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838296

It depends on the collection, but for a dictionary the hash code is used to determine which bucket the object is added to, and Equals is used to find the item within the bucket, amongst other items which may have the same hash.

Upvotes: 1

Related Questions