I understand that one cannot use an enumerator and make amendments to List<T>
at the same time. Adding or removing an item will invalidate the enumerator(http://msdn.microsoft.com/en-us/library/system.collections.ienumerator(v=vs.110).aspx), since the internal array will be reallocated(http://msdn.microsoft.com/en-us/library/3wcytfd1(v=vs.110).aspx).
- Is it always true, even if
Count
is less thanCapacity
?(As far as I understand in this case the array won't be reallocated, so the enumerator has to be valid); - Why
Current
returns the element that it is set to, even if the enumerator is already invalidated?(I mean, if the array was reallocated...); - Does reallocation preservers the original order of the items? I mean if some item has index of n, will it have the same index after adding and item?(MSDN says that
Add
adds an object to the end of the list) If so, it is safe to run through a list in a regularfor
loop and to add the items assuming that the loop will iterate through each item only once, am I correct?