In .NET, there is a constructor for Dictionary
that takes one parameter, int capacity
. This is the same as with many other collecti
This is partially a guess: a Dictionary is "ordered" as a hash table. The capacity that is reserved, is not simply a bunch of free memory addresses on top of your Dictionary. Instead, it consists of empty room throughout the Dictionary. This is done to make adding / moving / removing etc very efficient. If you had a TrimExcess
method for Dictionary, the whole Dictionary would have to copy everything to a new location without any gaps between the elements.
Actually: the gaps should remain otherwise the benefit of a hash table becomes void, trimming (TrimExcess
), if implemented, should only trim the internal ValueCollection
.
Update: expanded and changed my ill-chosen words
Update: the BCL team says TrimExcess for Dictionaries "could be useful".
Update: the feature request resolved as Won't Fix: "Unfortunately, we won't be able to get to this for the next release of .NET, so I'm resolving this as Won't Fix."