Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

foreach on Collection, List, IList, ICollection, ...

4 views
Skip to first unread message

Dansk

unread,
May 15, 2008, 4:41:04 PM5/15/08
to
Hi all,

Can I assume that when I loop throw a Collection, List, IList,
ICollection, ... by using foreach, I first get the element of index 0,
then index 2, and so on until the last one ?

Thanks for your answers,

Dansk.

Jeremy Shovan

unread,
May 15, 2008, 4:49:51 PM5/15/08
to
No... It would go index 0 then index 1, index 2, so on.....


"Dansk" <da...@laouilest.fr> wrote in message
news:Otc4ovst...@TK2MSFTNGP04.phx.gbl...

Jeremy Shovan

unread,
May 15, 2008, 5:03:59 PM5/15/08
to
Actually, I would say that you can't depend on them starting at index 0 in a
foreach. The reason being that these are all interfaces and thus can be
implemented any way the programmer wants and you may have some developer
that for some odd reason likes to start at index 1. However in order for you
to use foreach on a collection that collection has to implement the
IEnumerator interface so I think you can be pretty sure that even if it
doesn't start at index 0 you will still hit each element in the collection.

If you are really that concerned about it starting at 0 then I would suggest
using a for loop instead.

"Dansk" <da...@laouilest.fr> wrote in message
news:Otc4ovst...@TK2MSFTNGP04.phx.gbl...

Jon Skeet [C# MVP]

unread,
May 15, 2008, 5:28:23 PM5/15/08
to
Jeremy Shovan <jer...@shovans.com> wrote:
> Actually, I would say that you can't depend on them starting at index 0 in a
> foreach. The reason being that these are all interfaces and thus can be
> implemented any way the programmer wants and you may have some developer
> that for some odd reason likes to start at index 1. However in order for you
> to use foreach on a collection that collection has to implement the
> IEnumerator interface so I think you can be pretty sure that even if it
> doesn't start at index 0 you will still hit each element in the collection.
>
> If you are really that concerned about it starting at 0 then I would suggest
> using a for loop instead.

Alternatively, consult the documentation for whatever collection you're
looking at. For instance, the documentation for List<T> makes it fairly
clear that you'll get the results in the natural order.

I can't remember ever being in a situation where a collection *had* a
natural order but didn't return the elements in that order from the
iterator. The benefits to readability of using foreach outweigh the
conservatism in this case, IMO.

--
Jon Skeet - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Khamis Abuelkomboz

unread,
May 15, 2008, 6:45:54 PM5/15/08
to Dansk

Yes, you can expect this. This is an elementary requirement of the enumerator functionality.

--
Try Code-Navigator on http://www.codenav.com
a source code navigating, analysis and developing tool.
It supports following languages:
* C/C++
* Java
* .NET (including CSharp, VB.Net and other .NET components)
* Classic Visual Basic
* PHP, HTML, XML, ASP, CSS
* Tcl/Tk,
* Perl
* Python
* SQL,
* m4 Preprocessor
* Cobol

Dansk

unread,
May 16, 2008, 3:37:14 AM5/16/08
to

Thank you for your answers, they really make sense to me.


Dansk a écrit :

0 new messages