问题
When adding a lot of elements in System.Collections.Generic.List<T>
it is running slow because when nums increases capacity it must copy all elements.
In C++ this is fixed with vector.reserve(n)
. How can i do that in C#?
回答1:
Use Capacity property:
list.Capacity = n;
or you can set initial capacity via the constructor:
var list = new List<int>(n);
来源:https://stackoverflow.com/questions/31118111/is-in-c-sharp-list-something-like-vector-reserven-in-c