EnsureCapacity should not have if (newCapacity < min) newCapacity = min;
viveknuna opened this issue · 1 comments
viveknuna commented
I think if (newCapacity < min) newCapacity = min; should be removed, I have added items to the full capacity of the list but never hit breakpoint on line newCapacity = min;, Can someone tell the purpose of this line as well?
private void EnsureCapacity(int min) {
if (_items.Length < min) {
int newCapacity = _items.Length == 0? _defaultCapacity : _items.Length * 2;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
svick commented
Without that line, the array would grow, but it would not honor min
. For example, if you have a list with Capacity = Length = 4
and AddRange
a collection with 6 items, removing that line would grow the list to 8, when it needs to grow to 10.
Note that the equivalent .Net 7 source contains a comment explaining this.