You can change List<T>
instance unlimited number times as well as other similar array-based structures in .net. Right, you can’t add more than int.MaxValue
values (actually a little bit less) but you can remove and next add so many times as you wish but of cause not exceeding List<T>.Capacity
value (int MaxArrayLength = 0X7FEFFFFF) and available memory.
If to look at List<T>.Add
implementation you will see the code version++
which means that list instance version is changed when a new element is added. The same happens with every list modification (Add
, Remove
, Insert
and so on). But when the version will be int.MaxValue
increment operator version++
won’t throw an exception because by default overflow-checking is suppressed. Perhaps this fact moved you to int.MaxValue
answer.
Also this line _items[_size++] = item
is not a problem because _size is decremented when you remove items. And next line if (_size == _items.Length) EnsureCapacity(_size + 1)
has no limitations too.