More about iterator

T

Tony Johansson

Hello!

I have noticed that I can use iterator in a method and return an IEnumerable
like this
public IEnumerable<T> Reverse()
{
for (int i = data.Count -1 ; i >=0 ; i--)
yield return data;
}

I can test this Reverse by having this code
foreach (string word in bc.Reverse())
Console.WriteLine(word);

Now to my question If I instead have defined the Reverse method like this
public IEnumerator<T> Reverse()
{
for (int i = data.Count -1 ; i >=0 ; i--)
yield return data;
}
having IEnumerator as the return type I get compile
error saying "foreach statement cannot operate on variables of type
'System.Collections.Generic.IEnumerator<string>' because
'System.Collections.Generic.IEnumerator<string>' does not contain a public
definition for 'GetEnumerator'"

So I wonder how is the connection between a returning type for IEnumerable
and IEnumerator for a method using iterator and the statement call that is
used after the in keword in a foreach loop construction.

//Tony
 
J

Jon Skeet [C# MVP]

So I wonder how is the connection between a returning type for IEnumerable
and IEnumerator for a method using iterator and the statement call that is
used after the in keword in a foreach loop construction.

foreach only works with an IEnumerable<T> or an IEnumerable (or in fact
another type which has a GetEnumerator() method returning something
which in turn has MoveNext() and Current, but that's quite a rare
situation).
 
T

Tony Johansson

Hello!

IEnumerator<T> IEnumerator<T>.GetEnumerator()
{
for (int i = 0; i < data.Count; i++)
yield return data;
}

The code in method GetEnumerator defines an iterator.
The compiler uses the code for generate an implementetion for class
IEnumerator<T> which
contain a Current method and a MoveNext method.

Now to my question if I instead have a method that return an IEnumerable
like the Reverse below.
I know what the compiler do if I have a GetEnumerator which I have described
above but what
will the compiler do when having a method like the Reverse ?
Can somebody describe in a way that I have written above.

public IEnumerable<T> Reverse()
{
for (int i = data.Count -1 ; i >=0 ; i--)
yield return data;
}


//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
IEnumerator<T> IEnumerator<T>.GetEnumerator()
{
for (int i = 0; i < data.Count; i++)
yield return data;
}

The code in method GetEnumerator defines an iterator.
The compiler uses the code for generate an implementetion for class
IEnumerator<T> which
contain a Current method and a MoveNext method.
Yup.

Now to my question if I instead have a method that return an IEnumerable
like the Reverse below.
I know what the compiler do if I have a GetEnumerator which I have described
above but what
will the compiler do when having a method like the Reverse ?
Can somebody describe in a way that I have written above.

public IEnumerable<T> Reverse()
{
for (int i = data.Count -1 ; i >=0 ; i--)
yield return data;
}


Again, it generates another class. In fact, that new class will
implement IEnumerable<T>, IEnumerable, IEnumerator<T>, IEnumerator and
IDisposable. The important thing is that it implements IEnumerable<T>
though, as that's what you've said that your method will return. The
details of *exactly* how it implements IEnumerable<T> and IEnumerator
<T> at the same time usually aren't important.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top