Hi songie,
If you call Remove or RemoveAt method you remove the element of the
collection, thus all the elements on the right side of the removed one shift
left 1 position (left or right ofcourse is completely subjective)
Anyway the indexes always are 0,1,2,....,Count - 1 without gaps in-between.
If you want to keep the count (assuming that you have collection of
references ) you can do
myList[k] = null; in this case you don't remove anything an no shifting
occurs.
However
for(int i=0; i<List.Count; i++) almost never works with removing elements
because anytime you remove an element you skip one (if you don't do
correction of 'i' ofcourse).
But if you do
for(int i = List.Count - 1; i >=0 ;i--)
this will work in all the cases as far as removing is concerned.
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"songie D" <an*******@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
If you remove an item from a collection (that is derived from
CollectionBase and thus implements IList) will the index property automatically 'shuffle' up one?
i.e. if of {0, 1, 2, 3} I delete item 2, then will I be left with {0, 1,
2} or {0, 1, 3} ? Basically I am implementing a custom collection and wondered if I could
rely on the fact that for(int i=0; i<List.Count; i++) would always be an
appropriate loop.