Refer to msdn to learn iterator

Source: Internet
Author: User

 

An iterator is a method, get accessor operator, which usesYieldKeyword to perform custom iteration on the array or collection class. The yield Return Statement will cause the elements in the source sequence to be returned to the caller immediately before accessing the next element in the source sequence. Although you write an iterator as a method, the compiler converts it into a nested class that is actually a state machine. As long as the foreach loop in the client code continues, this class will track the position of the iterator.

 

The foreach statement is used to call the iterator from the client code. When creating an iterator for a class or structure, you do not have to implement the entire ienumerator interface. When the compiler detects the iterator, it automatically generates the current, movenext, and dispose methods of the ienumerator or ienumerator <(of <(T>) interface.

 

An iterator is a piece of code that returns an ordered sequence of values of the same type.

The iterator can be used as a method, operator, or get accessors code body.

The iterator Code uses the yield return statement to return each element in sequence. Yield break terminates iteration.

Multiple iterators can be implemented in the class. Each iterator must have a unique name like any class member and can be called by the client code in the foreach statement, as shown below: foreach (int x in sampleclass. iterator2 ){}. The return type of the iterator must be ienumerable, ienumerator, ienumerable <(of <(T>), or ienumerator <(of <(T>)> ).
The iterator is the basis for delayed execution behavior in the LINQ query.
The yield keyword is used to specify one or more returned values. When the yield Return Statement is reached, the current position is saved. The next time you call the iterator, it will start execution again from this position.

The iterator is particularly useful for collection classes. It provides a simple method to iterate complex data structures (such as binary trees ).

In the following two custom collection classes, the use of the iterator can greatly reduce the amount of code used.

 

Using system;
Using system. collections;

Namespace yield
{
Class week: ienumerable
{
String [] days = {"1 day of the week", "2 days of the week", "3 days of the week", "4 days of the week", "5 days of the week", "6 days of the week ", "7 days of the week "};
Public ienumerator getenumerator ()
{
For (INT I = 0; I <days. length; ++ I)
{
Yield return days [I];
}
}
}
Class Test
{
Static void main ()
{
Week = new week ();
Foreach (string day in week)
{
Console. writeline (day );
}
Console. readkey ();
}
}
}

Re-use the iterator to implement the previous custom Library (SET) Class
Using system. collections;
Using system;

Namespace Library
{
Class Library: ienumerable
{
Arraylist books;
Public Library ()
{
Books = new arraylist ();
}
Public void remove (string book)
{
Books. Remove (book );
}
Public void add (string book)
{
Books. Add (book );
}
Public ienumerator getenumerator ()
{
// For (INT I = 0; I <books. Count; I ++)
//{
// Yield return books [I];
//}
Foreach (string book in books)
{
Yield return book;
}
}
}
Class Test
{
Static void main ()
{
Library Lib = new library ();
Lib. Add ("advertisement");
Lib. Add ("Communication Studies");
Lib. Add ("Media Research");
Lib. Add ("Market Research");
Console. writeline ("the textbooks included include :");
Foreach (string name in Lib)
{
Console. writeline (name );
}
Console. readkey ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.