473,320 Members | 1,694 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,320 software developers and data experts.

Collection order

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
Nov 16 '05 #1
8 6511
-----Original Message-----
If I create a collection and add members using the Add() method, is isguarenteed that the members will be returned in the same order that theywere 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
Nov 16 '05 #2
> >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.


Thanks for your help, I understand now.

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

Mike
Nov 16 '05 #3
Hello!
With regards to ordering, what will the behaviour be of a class that
inherits from CollectionBase?


It is ordered!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #4
> > With regards to ordering, what will the behaviour be of a class that
inherits from CollectionBase?


It is ordered!


Thanks... how can you tell this?

Mike
Nov 16 '05 #5
Hi,
"Anders Borum" <a@b.dk> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl...
Hello!
With regards to ordering, what will the behaviour be of a class that
inherits from CollectionBase?


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,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #6
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
With regards to ordering, what will the behaviour be of a class that
inherits from CollectionBase?


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


There's a difference between "sorted" and "ordered". A CollectionBase
*is* ordered, but *isn't* sorted.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
> > 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.


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

Mike
Nov 16 '05 #8
Mike <no***@hotmail.com> wrote:
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.


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


No, because not all collections derive from CollectionBase.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

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

Similar topics

3
by: GrumpyDev | last post by:
what is the best way to implement collection of custom entities?
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
1
by: Dan H. | last post by:
Hello, I have an application that requires a collection that is sorted by a double field first, and then by a long field. So, lets say I have an class that has 2 fields called time, priority. I...
11
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
0
by: daniel li | last post by:
I got another convert issue. This is the same project as I posted yesterday, and received a perfect response. The conversion threw a SupportClass for me and one of the data type is...
10
by: aa7im | last post by:
I have a similiar thread going about this topic but I decided to break it off into a seperate discussion. Question: What is the best way to determine if an object "IsNew" to a collection? ...
1
by: dx | last post by:
Any ideas? I can't find any examples that support this. I'm thinking a solution may be something like inherting arraylist and containing a hashtable. or Inheriting NameObjectCollectionBase...
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...
2
by: BSamp | last post by:
I have the following business entity classes shown below. I have a data layer that retrieves the data from the database and populates a new instance of the PendingRecord class then adds it to the...
4
by: Sugandh Jain | last post by:
Hi, I am developing using MS Visual Studio .net 2005, c# 2.0. My query/confusion is as follows: If I have a collection of a particular type of object. While moving in the collection using...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.