Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set initial capacity to a SortedDictionary?

I'm looking into code optimization of a project of mine and I was wondering how can I set an initial capacity to a SortedDictionary. I know that I can do it to a List and a Dictionary pretty easily in the constructor. But how can I do it to a SortedDictionary?

I thought about doing it like the code below, but I'm not sure if that works and how I could check it, once Dictionary has no capacity property (or has it?).

int capacity = 1000;

SortedDictionary<TKey, TValue> exampleSortedDictionary = new SortedDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(capacity));
like image 974
Lucas Ferreira Avatar asked Oct 17 '25 23:10

Lucas Ferreira


1 Answers

You have no need to specify initial capacity for SortedDictionary<K, V>.

Please note, that Dictionary<K, V> is a hash table that's why the initial capacity (say, 1000 "rows") is quite reasonable. Unlike Dictionary<K, V>, SortedDictionary<K, V> is a red-black tree for which capacity has no meaning.

like image 134
Dmitry Bychenko Avatar answered Oct 19 '25 13:10

Dmitry Bychenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!