If I have, say, 100 items that'll be stored in a dictionary, should I initialise it thus?
var myDictionary = new Dictionary<Key, Value>(100);
My understanding is that the .NET dictionary internally resizes itself when it reaches a given loading, and that the loading threshold is defined as a ratio of the capacity.
That would suggest that if 100 items were added to the above dictionary, then it would resize itself when one of the items was added. Resizing a dictionary is something I'd like to avoid as it has a performance hit and is wasteful of memory.
The probability of hashing collisions is proportional to the loading in a dictionary. Therefore, even if the dictionary does not resize itself (and uses all of its slots) then the performance must degrade due to these collisions.
How should one best decide what capacity to initialise the dictionary to, assuming you know how many items will be inside the dictionary?