473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Collection indexes

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.
Nov 16 '05 #1
3 3150
Hi

The index will be updated dynamically.

--
Ravikanth[MVP]

"songie D" wrote:
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.

Nov 16 '05 #2
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.
Nov 16 '05 #3
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) [C# MVP]" <10*@100.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mel | last post by:
I have a large collection of custom objects, each representing a period in time with each having a start datetime and an end datetime. I frequently need to query this collection to return a subset...
0
by: Mat | last post by:
When users move listview columns, does columns indexes changes? i access columns in the collection using indexes and i am afraid.;-(
9
by: Jeff Mason | last post by:
I'm trying to define a collection which inherits from DictionaryBase. This collection requires a composite key (comprised of two string values) and the value associated with the key is an integer....
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
2
by: Trevor | last post by:
This works in Firefox, but not in IE. for (var i in appraisertable.rows) { var row = appraisertable.rows; alert(row.id); } IE displays "undefined" and gives the error "id is null or not an...
5
by: gmccallum | last post by:
I am trying to create a base class that contains a collection that MUST be able to be referenced by both a string key and a positional index. This collection MUST keep the items in the same order...
4
by: Sjaakie | last post by:
I need to replace parts of a string, in a collection deserialized from an XML file, with values from another collection. Is there another, more clever/faster/better, method than the loops below? ...
10
by: lesperancer | last post by:
you start with a small application in access97, then you have more modules and more... and you reach the point where tables like 'item' and 'employee' reach the limit and you know there's more...
5
by: Simon Woods | last post by:
Hi Does anyone know if there any dotnet software out there which converts a dataset into a collection of custom objects, programmatically i.e. actually creates the class structure and collection...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.