473,473 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Are collections ordered and other questions

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

Nov 16 '05 #1
2 1209
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

Nov 16 '05 #2
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" <pd******@NOSPAM.hotmail.com> wrote in message
news:-b********************@giganews.com...
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


Nov 16 '05 #3

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

Similar topics

6
by: Paul Delhanty | last post by:
Hi, I am converting an existing native C++ program making heavy use of STL to C#2.0 with STL usage replaced by the new generic collections. The conversion has gone well to the point where I am...
5
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a...
3
by: Paul | last post by:
Hello, I'm upgrading a small single user app to VB.NET and have a few questions about loading classes & collections classes with the data from a one to many dataset structure. I'm serializing...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
8
by: Fuzzyman | last post by:
Sorry for this hurried message - I've done a new implementation of out ordered dict. This comes out of the discussion on this newsgroup (see blog entry for link to archive of discussion). See...
11
by: CMM | last post by:
First let me say that maybe I'm having a "duh" moment and perhaps I'm missing something... but it seems to me that no one thing in the System.Collections namespace (even in .NET 2.0) even comes...
1
by: dnvhari | last post by:
Hi all, we have an xml file. Based on this file we have to generate menu. This xml file changes every day. We are loading the xml file into the application memory and transforming the data of...
12
by: Gilbert | last post by:
H, i'm starting with asp.net/vb.net and have some questions about arrays and collections: 1) what's the difference between: dim x() as string and dim x as array
0
by: Ben Finney | last post by:
Armin Ronacher <armin.ronacher@active-4.comwrites: A welcome addition. Since we're not proposing a built-in type, could we choose a name that is more explicit, and consistent with the types...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.