Collection order

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

If I create a collection and add members using the Add() method, is is
guarenteed that the members will be returned in the same order that they
were added, if:

i) I enumerate using a foreach loop?
ii) I index from 0 to Count-1 using a for loop?

Cheers,

Mike
 
-----Original Message-----
If I create a collection and add members using the Add() method, is is
guarenteed that the members will be returned in the same order that they
were added, if:

i) I enumerate using a foreach loop?
ii) I index from 0 to Count-1 using a for loop?

No, it's not. Some collections are ordered, and others
are not: it depends on the semantics of the collection.
Some collections, such as ArrayList will return the
collection in order. Others such as Hashtable will return
items according to their hash value; Dictionary will sort
by the key value.

-mark
 
If I create a collection and add members using the Add()
method, is is

No, it's not. Some collections are ordered, and others
are not: it depends on the semantics of the collection.
Some collections, such as ArrayList will return the
collection in order. Others such as Hashtable will return
items according to their hash value; Dictionary will sort
by the key value.

Thanks for your help, I understand now.

With regards to ordering, what will the behaviour be of a class that
inherits from CollectionBase?

Mike
 
Hi,


Anders Borum said:
Hello!


It is ordered!


I really don;t think that, CollectionBase use an ArrayList instance for
storing the values , if ArrayList is not sorted ( as Colburn's said ) then
the CollectionBase is by default not sorted.

You can sort it , though , just including a Sort method that return a new
collection sorted. This is very easy to do

Cheers,
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
I really don;t think that, CollectionBase use an ArrayList instance for
storing the values , if ArrayList is not sorted ( as Colburn's said ) then
the CollectionBase is by default not sorted.

You can sort it , though , just including a Sort method that return a new
collection sorted. This is very easy to do

There's a difference between "sorted" and "ordered". A CollectionBase
*is* ordered, but *isn't* sorted.
 
You can sort it , though , just including a Sort method that return a
new
There's a difference between "sorted" and "ordered". A CollectionBase
*is* ordered, but *isn't* sorted.

So if CollectionBase is ordered, then all collections are ordered?

Mike
 

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

Back
Top