Should a .NET generic dictionary be initialised with a capacity equal to the number of items it will contain?

前端 未结 6 1924
小鲜肉
小鲜肉 2020-12-17 17:12

If I have, say, 100 items that\'ll be stored in a dictionary, should I initialise it thus?

var myDictionary = new Dictionary(100);
<         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 17:45

    I think you're over-complicating matters. If you know how many items will be in your dictionary, then by all means specify that on construction. This will help the dictionary to allocate the necessary space in its internal data structures to avoid reallocating and reshuffling data.

提交回复
热议问题