Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In which condition do we typically override Get Hash Code method?
How exactly overriding this method helps us?
Posted

1 solution

Some types of collection use the hash code of objects to expedite searches and other operations. For these collections to work correctly, it is necessary that any objects which should be regarded as identical must return the same hash code. Objects which should not be regarded as identical should be unlikely to return the same hash code. If two objects in a collection are different, but return the same has code, such an occurrence is called a hash collision. Occasional hash collisions are often inevitable, but overly-frequent hash collisions will degrade performance.

Generally, one should override Object.GetHashCode under essentially the same circumstances as one would override Object.Equals; if Object.Equals is overridden so that it could return 'equals' with two distinct objects (as could happen with e.g. strings) then Object.GetHashCode must be overridden so that it will return the same value for any and all objects that are equal to each other.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900