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#?
C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.
Use Capacity property:
list.Capacity = n;
or you can set initial capacity via the constructor:
var list = new List<int>(n);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With