Are collections ordered and other questions

R

Rachel Suddeth

Every time I read about these things my brain gets muddled and I can't keep
them straight. I'm going to give my guesses, could someone confirm or deny?

I think a collection doesn't have to be ordered. But you do have to be able
to use foreach(), and it will hit every object in the collection once. But
could be in one order one time, and in another order the next, even if
nothing is done by the programmer to change anything in the collection.
(Probably wouldn't but no guarantees.)

I think if a collection has an indexer, then it is ordered. I don't mean
sorted, but If I say something[3], at any time in my program, and no
programmer has ever done anything intentionally to change the contents of
something[3], or add anything before postition 3, or remove what's at
position[3], then I will get the same results. Can a group of like objects
be indexed, but not be a collection?

I think the ControlCollection is ordered in this way, since it has an
AddAt(index) method. Does that mean it's indexed?
______________________________________________________________

Roydan Enterprises Ltd
602 North 9th Street
Manitowoc, WI 54220-3924
 
P

Pete Davis

It depends on the collection, but most if the built-in collections are in
the order that items are added. You can access most collections via an
indexer property. In the documentation you'll see "Item" as a property. This
is the indexer.

So, for example, in the control collection you can do something like:

Control firstControl = myForm.Controls[0];

While there's no rule requiring it that I'm aware of, foreach will usually
return things in the same order every time. I suppose it's possible for a
collection to alter this, though I'm not sure. There may be requirements
that prevent it, but nothing comes to mind.

But keep in mind, collections are simply implementations of the ICollection
interface. This in turn is derived from IEnumerable. So as long as the
implementation follows the rules required by those two interfaces, then it
can actually do whatever it wants "under the hood". foreach uses the
enumerator to traverse the collection.

IEnumerable just returns an IEnumerator. When calling IEnumerator.Reset(),
the enumerator is supposed to be set "to it's initial position, which is
before the first element in the collection." But there's nothing to indicate
that the "first element" can't be different every time you call Reset(). I
can't think of why one would implement it in a way such that the order
differed each time through a foreach, though.

Pete
 
R

Rachel Suddeth

It is a bit hard to imagine the order would change if you really didn't add
or take anything away. It's not unthinkable that some shuffling could occur
in adding or deleting an item...
Still I hate to count on it if there's no rule to enforce it. Who knows what
goofy things some unseen optimizer could do behind the scenes? Then again,
why count on nothing being added or deleted. Who knows what goofy things
some programmer may want to do in future, not knowing I was counting on
cycling twice, getting things in the same order...

Pete Davis said:
It depends on the collection, but most if the built-in collections are in
the order that items are added. You can access most collections via an
indexer property. In the documentation you'll see "Item" as a property.
This
is the indexer.

So, for example, in the control collection you can do something like:

Control firstControl = myForm.Controls[0];

While there's no rule requiring it that I'm aware of, foreach will usually
return things in the same order every time. I suppose it's possible for a
collection to alter this, though I'm not sure. There may be requirements
that prevent it, but nothing comes to mind.

But keep in mind, collections are simply implementations of the
ICollection
interface. This in turn is derived from IEnumerable. So as long as the
implementation follows the rules required by those two interfaces, then it
can actually do whatever it wants "under the hood". foreach uses the
enumerator to traverse the collection.

IEnumerable just returns an IEnumerator. When calling IEnumerator.Reset(),
the enumerator is supposed to be set "to it's initial position, which is
before the first element in the collection." But there's nothing to
indicate
that the "first element" can't be different every time you call Reset(). I
can't think of why one would implement it in a way such that the order
differed each time through a foreach, though.

Pete

Rachel Suddeth said:
Every time I read about these things my brain gets muddled and I can't keep
them straight. I'm going to give my guesses, could someone confirm or deny?

I think a collection doesn't have to be ordered. But you do have to be able
to use foreach(), and it will hit every object in the collection once.
But
could be in one order one time, and in another order the next, even if
nothing is done by the programmer to change anything in the collection.
(Probably wouldn't but no guarantees.)

I think if a collection has an indexer, then it is ordered. I don't mean
sorted, but If I say something[3], at any time in my program, and no
programmer has ever done anything intentionally to change the contents of
something[3], or add anything before postition 3, or remove what's at
position[3], then I will get the same results. Can a group of like
objects
be indexed, but not be a collection?

I think the ControlCollection is ordered in this way, since it has an
AddAt(index) method. Does that mean it's indexed?
______________________________________________________________

Roydan Enterprises Ltd
602 North 9th Street
Manitowoc, WI 54220-3924
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top