Initial capacity of collection types, e.g. Dictionary, List

前端 未结 4 893
春和景丽
春和景丽 2020-11-29 05:56

Certain collection types in .Net have an optional \"Initial Capacity\" constructor parameter. For example:

Dictionary something = new          


        
4条回答
  •  悲哀的现实
    2020-11-29 06:14

    If you know the size, then tell it; a minor optimisation in most "small" cases, but useful for bigger collections. I would mainly worry about this if I am throwing a "decent" amount of data in, as it can then avoid having to allocate, copy and collect multiple arrays.

    Most collections indeed use a doubling strategy.

提交回复
热议问题