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.

About iterator and yield

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Johansson - 07 Mar 2008 16:35 GMT
Hello!

In C# 2.0 has iterator been added and this posting is about that.

Here is a method that has implemented GetEnumerator without using iterator
IEnumerator<T> IEnumerator.GetEnumerator()
{
  return new TreeEnumerator<T>(this);
}
There is also another class called TreeEnumerator which has implemented the
IEnumerator<T> interface.
This is easy to understand.

Below I have a class called BasicCollection<T> which use iterator.

Now to my first question:  This method:
IEnumerator<T> IEnumearble<T>.GetEnumerator()
{
     for (int i = 0;  i<data.Count; i++) yield return data[i];
}
implement the IEnumarable<T> interface and it return an
IEnumerator<T> according to the return type but it doesn't look like that.
Can somebody explain that ?

I test this by writing this code and it writes them out in the same order
that I inserted them into FillList which is correct.
BasicCollection<string> bc = new BasicCollection<string>();
bc.FillList("Twas", "brillig", "and", "the", "slithy", "toves");
foreach(string word in bc);
  Console.WriteLine(word);

Assume I want to write them out in the Reverse sequence I can then write
this method which has been found in a book
public IEnumerable<T> Reverse()
{
   get
   {
      for ( int i = data.Count -1 ; i>= 0;  i--)
        yield return data[i];
   }
}

To test this I can write this test code and it do writes then in the revers
sequence which in correct.
BasicCollection<string> bc = new BasicCollection<string>();
bc.FillList("Twas", "brillig", "and", "the", "slithy", "toves");
foreach(string word in bc.Reverse);
  Console.WriteLine(word);

Now to my second question:
I can't understand this. In the first method that used iterator that was
called GetEnumerator it said yield return data[i];
and this caused a IEnumerator<T> to be returned.
In the second example that uses iterator it said yield return data[i]; and
now it return an IEnumerable<T>
So the same statement will return different interface types.

class   BasicCollection<T>  :  IEnumerable<T>
{
  private List<T> data = new List<T>();

      public void FillList(params T [] items)
     {
         for (int i = 0;  i < items.Length; i++)
            data.Add(items[i]);
     }

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

//Tony
Jon Skeet [C# MVP] - 07 Mar 2008 16:44 GMT
On Mar 7, 4:35 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> In C# 2.0 has iterator been added and this posting is about that.
>
[quoted text clipped - 17 lines]
> IEnumerator<T> according to the return type but it doesn't look like that.
> Can somebody explain that ?

Well, that's basically what iterator blocks are all about. Your code
doesn't explicitly return the iterator, it returns the data, one datum
at a time. The compiler builds a state machine to handle the
iteration.

<snip>

> Now to my second question:
> I can't understand this. In the first method that used iterator that was
> called GetEnumerator it said yield return data[i];
> and this caused a IEnumerator<T> to be returned.
> In the second example that uses iterator it said yield return data[i]; and
> now it return an IEnumerable<T>

Iterator blocks apply to members returning *either* an IEnumerator
*or* an IEnumerable. Both work. The same compiler magic applies to
both ways of doing things.

See this article for more about iterator blocks (and LINQ):

http://www.eggheadcafe.com/tutorials/aspnet/159e4793-6b17-4e89-bd94-3bde8a5f2d50
/iterators-iterator-block.aspx


Jon

Rate this thread:







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.