I’m not sure if this is the right question to ask here but please don’t kill me 🙂
I have an argument with a friend about C#’s dictionary…
She tells me that if I have lets say dictionary with 1 element. And the hash code for the key is 100000 then the internal array of the dictionary will be at the size of 100000!
Is it true? I tried to find answers on Google but for some reason I didn’t find for that question.
The default constructor of Dictionary, “has the default initial capacity”, according to MSDN.
It also states:
One such constructor simply takes an
Int32, which initialises the internal storage as follows:What the “default initial capacity” of the dictionary actually is is an internal implementation detail of the class and as such not exposed in the documentation or public API.
Disassembling
mscorlibwithilspyand examining the default constructor shows that it is implemented as follows:That chained constructor is implemented as follows:
i.e.
Initialize()is not called at all by the default constructor, either directly or indirectly.Initialize()is the method that sets up the internal storage.So actually, if you call the default constructor, the internal storage size is not even initialised until you first add an item. So it has essentially a zero size.
Initialize()is eventually called with a value of zero the first time you call.Add(),which sets things up.
GetPrime(0)returns3, sothis.bucketsis set to an array containing three integers.The line that assigns a value to
this.entrieslooks a little odd but I don’t see where 100000 comes into this.Short answer
I think your colleague is wrong.