Reputation: 856
A List has separate properties for getting the Count and its Capacity. Dictionaries, like all collections, have the Count property too, and it has a capacity because it has several constructors that allow you to specify it and the documentation for the Add method mentions it. However, i don't see any way of querying the Dictionary for what its current capacity is.
Even if there is no way of getting a Dictionary's current capacity, is there any way to predict when a reallocation might occur?
Upvotes: 5
Views: 2183
Reputation: 30022
Dictionaries do not work like lists exactly. If you examine the source code provided by microsoft. You can find multiple private fields that might be helpful.
Be aware that this is an encapsulated implementation detail, you should not depend on it in your production code as names, behavior of private and internal members might change without prior notice!
You have internal arrays int[] buckets
and Entry[] entries
. You also have int freeList
and int freeCount
. You can use reflection to play around these.
To answer your question, YES a reallocation is triggered on each insert and here is the actual code:
int index;
if (freeCount > 0)
{
index = freeList;
freeList = entries[index].next;
freeCount--;
}
else
{
if (count == entries.Length)
{
Resize();
targetBucket = hashCode % buckets.Length;
}
index = count;
count++;
}
Upvotes: 1