Collection indexes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 o
the fact that for(int i=0; i<List.Count; i++) would always be an appropriate loop.
 
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 said:
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.
 
Thanks, that's what I wanted to know.
I'm probably going to be doing removing by object.
This is only for the purposes of the enumerator.

Stoitcho Goutsev (100) said:
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 said:
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.
 
Back
Top