When a List<T>
gets full, it doubles in size, occupying twice the memory, but would it automatically decrease in size if you removed elements from it?
As much as I understand decreasing the Capacity
would not mean relocating all the data in memory, it would just need to drop off the end of the reserved memory, but does it actually do it?
You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output. You can also use negative index values.
You can use the pop() method to remove an element from the array.
No, List
doesn't ever decrease the capacity unless you explicitly lower it yourself by setting that property or using TrimExcess
, except when you call Clear
and it can remove the buffer entirely.
Of course, that's just the current implementation, and it is an implementation detail, so you cannot rely on it not shrinking the backing array.
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