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

前端 未结 6 1921
小鲜肉
小鲜肉 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:37

    Yes, contrary to a HashTable which uses rehashing as the method to resolve collisions, Dictionary will use chaining. So yes, it's good to use the count. For a HashTable you probably want to use count * (1/fillfactor)

提交回复
热议问题