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" <ra****@bldhound.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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