Hash collisions are correctly handled by Dictionary<>
- in that so long as an object implements GetHashCode()
and Equals()
correctly, the appropriate instance will be returned from the dictionary.
First, you shouldn't make any assumptions about how Dictionary<>
works internally - that's an implementation detail that is likely to change over time. Having said that....
What you should be concerned with is whether the types you are using for keys implement GetHashCode()
and Equals()
correctly. The basic rules are that GetHashCode()
must return the same value for the lifetime of the object, and that Equals()
must return true when two instances represent the same object. Unless you override it, Equals()
uses reference equality - which means it only returns true if two objects are actually the same instance. You may override how Equals()
works, but then you must ensure that two objects that are 'equal' also produce the same hash code.
From a performance standpoint, you may also want to provide an implementation of GetHashCode()
that generates a good spread of values to reduce the frequency of hashcode collision. The primarily downside of hashcode collisions, is that it reduces the dictionary into a list in terms of performance. Whenever two different object instances yield the same hash code, they are stored in the same internal bucket of the dictionary. The result of this, is that a linear scan must be performed, calling Equals()
on each instance until a match is found.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…