Re: recursive GetEnumerator()



On Jun 25, 12:04 pm, "colin" <colin.ro...@xxxxxxxxxxxxxxxxxx> wrote:
Yes, it does return an enumerator. (Or, at least, it works as if it
returns an enumerator. I don't know exactly how it's implemented.)

it seems to actually return the data not the enumerator,
well it complains if you do,
I gues IEnumerator is just a sort of keyword to say its not a normal
function.

No, not at all. GetEnumerator returns an enumerator. That enumerator
is then used to fetch an IEnumerable, which has the MoveNext and
Current members.

foreach does all this for you under the hood.

im not sure what happens if you actually return data without yield,
wether it gets called for each element, I gues thats what the Next and
Current would be for.

yield return is just smart syntactic sugar which builds an IEnumerator/
IEnumerable.
You can use ildasm or reflector to see what types are in your compiled
code.

It's worth understanding what foreach does before you embark on C# 2's
iterator-building features.

Jon

.



Relevant Pages

  • Re: [Feature Request] "first:" "last:" sections in a "foreach" block
    ... >> I'm somewhat influenced by the fact that I tend to use 'foreach' when I ... >> to iterate over all of the elements but don't care which order they come ... While IEnumerator has the same suggestion, ... > the enumerator should return elements in that order. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: for vs foreach for array enumeration
    ... An enumerator is an object that implements the IEnumerator interface; ... MoveNext() must then be called to get to the first item) ... yield return item; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Returning an IEnumerator from a ServicedComponent
    ... > channel sink which means either the server has no registered server ... > Do I need to apply different attributes to the Enumerator class? ... somehow from MarshalByRefObject (usually done in your case by creating a ... and contained an implementation of IEnumerator that was created ...
    (microsoft.public.dotnet.framework.component_services)
  • Re: Interface features implemented without using Interface
    ... public object Clone() {... ... Enumerator (IEnumerable and IEnumerator). ... complain 'cause the class "does not implement the GetEnumerator() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach enhancement
    ... >> supporting IEnumerable into the array without splitting it up into its ... Better would be IEnumerator instead of IEnumerable, ... > 1...1000) I see no reason to generate an object, instead a for loop like ... and for more complicated things I would create my own enumerator. ...
    (microsoft.public.dotnet.languages.csharp)

Loading