Re: [Feature Request] "first:" "last:" sections in a "foreach" block

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 04/05/04


Date: Mon, 5 Apr 2004 20:09:31 +0100

Dilip Krishnan <dilipdotnet..NOSPAM..@apdiya.com> wrote:
> I would think that by the definition of foreach, the language construct
> is unable to predict the size of the enumerable in question. Which would
> mean that the foreach loop would need to read-ahead to determine the
> ordinality of the enumerable list. IMHO that would make the whole
> foreach inefficient would'nt you say?

You don't need to know the ordinality though - you only need to know
first or last. First is obvious, and the "last" bit you just execute
when the next MoveNext returns false (or whatever - I can't remember
the exact interface offhand). It would be slightly trickier to make the
"last" case not also execute "other", and it would be easier to
basically make that something executed *as well as* the "other" case,
but other than that it's not tricky.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... > foreach means foreach means foreach, so for each item of a collection of ... It could be that I want to execute this ... Order returned by an enumerator is only statically defined during the ... Syntax and semantics are another thing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... foreach means foreach means foreach, so for each item of a collection of ... I would not like to see a switch inside the loop that test ... It could be that I want to execute this ... execute code for. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why doesnt foreach return a value
    ... the body of the foreach command never is executed. ... proc test { ... foreach a $l {incr itemcount} ... with foreach returning a value (or if the body doesn't execute) ...
    (comp.lang.tcl)
  • Re: threads
    ... This creates a single thread to execute the block. ... $threads->create and async has to do with whether you're compfortable ... foreach { ...
    (perl.beginners)
  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... I would think that by the definition of foreach, the language construct ... > I suggest adding four new keywords to ... > It is handled entirely by the compiler, so that it has ZERO effect on code ...
    (microsoft.public.dotnet.languages.csharp)