预分配列表c#

for*_*ect 4 c# data-structures

我在c#工作,我正在创建一个列表(newList),我可以从另一个列表(otherList)的长度.在c#中列表实现的方式是预先分配列表的长度对于使用otherList.Count的性能更好或者只使用newList.Add(obj)而不用担心长度?

Tre*_*ott 7

List<T>实现以下构造函数是为了提高像您这样的场景中的性能:

http://msdn.microsoft.com/en-us/library/dw8e0z9z.aspx

public List(int capacity)

只需传递构造函数中的容量即可.

newList = new List<string>(otherList.Count);