If I have, say, 100 items that\'ll be stored in a dictionary, should I initialise it thus?
var myDictionary = new Dictionary(100);
<
Specifying the initial capacity to the Dictionary
constructor increases performance because there will be fewer number of resizes to the internal structures that store the dictionary values during ADD operations.
Considering that you specify a initial capacity of k to the Dictionary
constructor then:
Dictionary
will reserve the amount of memory necessary to store k elements;From MSDN:
The capacity of a Dictionary(TKey, TValue) is the number of elements that can be added to the Dictionary(TKey, TValue) before resizing is necessary. As elements are added to a Dictionary(TKey, TValue), the capacity is automatically increased as required by reallocating the internal array.
If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Dictionary(TKey, TValue).