Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

More about iterator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Johansson - 07 Mar 2008 22:23 GMT
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];
}

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[i];
}
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
Jon Skeet [C# MVP] - 07 Mar 2008 22:35 GMT
<snip>

> 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).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Tony Johansson - 08 Mar 2008 11:16 GMT
Hello!

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

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[i];
}

//Tony

> <snip>
>
[quoted text clipped - 8 lines]
> which in turn has MoveNext() and Current, but that's quite a rare
> situation).
Jon Skeet [C# MVP] - 08 Mar 2008 13:45 GMT
> IEnumerator<T> IEnumerator<T>.GetEnumerator()
> {
[quoted text clipped - 6 lines]
> 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.
[quoted text clipped - 8 lines]
>          yield return data[i];
> }

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.