Re: [Feature Request] "first:" "last:" sections in a "foreach" block
From: James Curran (JamesCurran_at_mvps.org)
Date: 04/04/04
- Next message: ilPostino: "Re: launch a form from within another"
- Previous message: Andreas Håkansson: "Re: launch a form from within another"
- In reply to: Paul E Collins: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Next in thread: Daniel O'Connell [C# MVP]: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 4 Apr 2004 08:17:46 -0700
"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:c4ovj5$ij8$1@hercules.btinternet.com...
> 'foreach' and indexed 'for' are pretty much interchangeable.
Actually they aren't. foreach works for every class that implements
IEnumerable, which is small and easy to implement. Indexing requires
implementing IList which is more complicated. Many collection
implementations don't lend themselves well to indexing (anything which using
a tree instead of an array behind the scenes). Check out their desciption
pages in the MSDN. In the framework over twice as many classes implement
IEnumerable as IList.
>> I'm somewhat influenced by the fact that I tend to use 'foreach' when I
want
to iterate over all of the elements but don't care which order they come up
in <<
Just because there is no particular order to the items in the collection,
does not mean when being processed, the first and last do not need to be
processed differently. As in my example of adding the comma, often it's just
the fact that this item is the first processed is enough to require special
processing.
>What if it's the *second* iteration you want to treat differently;
> should there be a 'second' keyword as well, or some kind of 'foreach ...
> case' construct?
No. Needing to treat the first or last item differently is fairly common.
Needing to treat an item besides the first or last differently is much
rarer. Also, there is no simple implementation for treating a middle item
differently.
I guess the basic question is what is the point of having a foreach in the
language anyway, when it can always be replaced by a for or while statement?
If you see an advantage in having it in the language, you should see the
advantage of this.
- Next message: ilPostino: "Re: launch a form from within another"
- Previous message: Andreas Håkansson: "Re: launch a form from within another"
- In reply to: Paul E Collins: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Next in thread: Daniel O'Connell [C# MVP]: "Re: [Feature Request] "first:" "last:" sections in a "foreach" block"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|