Do C# dictionaries use hashing in the same manner as Java HashMaps?


I understand that Java HashMap elements are stored in “buckets” based on the hashes of the elements’ keys. Does that same hashing occur in C# dictionaries? If not, then how does lookup work?


@maytham-ɯɐɥʇʎɐɯ aren’t Dictionaries different from Hashtables, though?
– Justin Borromeo
19 hours ago
Yes, C# Dictionary<K, V> works the same way. Buckets are used to split the space of hashes into smaller, more manageable chunks, and deal with hash collisions. Number of buckets is the first prime number larger than the dictionary capacity (so, when you insert the first item into the dictionary it will contain 3 buckets), and the items are rearranged into new buckets whenever there is a need to increase the capacity.
Why are you asking, just in case this is a XY Problem –‌​m ?
The source code is available; if you want to know exactly how it works, read the sources.



There are several kind of Dictionarys inside C# System.Collections – namespace. They use different strategies to store theire internal data:

This one System.Collections.Specialized.HybridDictionary
uses DoubleLinkedLists until a certain size is reached, then switches to Hashsets. The “normal” System.Collections.Generics-Dictionary uses Hashsets internally all the time. There is also a Dictionary for concurrent uses – look it up yourself (Concurrent Dictionary)if you like.

So it depends on what kind of Dictionary you are using and (in above case) might change due to internal considerations of the class you are using for performance or other reasons.