Document directory 
C # iterator 
The iterator is a new feature in C #2.0. An iterator is a method, get accessor or operator that enables you to support foreach iteration in a class or structure without implementing the entire ienumerable interface. You only need to provide an iterator to traverse the data structure in the class. When the compiler detects the iterator, it automatically generatesCurrent,MoveNextAndDisposeMethod.
Iterator Overview 
 
 - 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. For more information, see yield. 
- 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, or ienumerator. 
The yield keyword is used to specify the returned value. 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 infrequently used data structures (such as binary trees ).
 
Simple Example
 
In this example,DaysOfTheWeekClass is a simple set class that stores the day of a week as a string. Each iteration of the foreach loop returns the Next string in the set.
 
public class DaysOfTheWeek : System.Collections.IEnumerable{    string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };    public System.Collections.IEnumerator GetEnumerator()    {        for (int i = 0; i < m_Days.Length; i++)        {            yield return m_Days[i];        }    }}class TestDaysOfTheWeek{    static void Main()    {        // Create an instance of the collection class        DaysOfTheWeek week = new DaysOfTheWeek();        // Iterate with foreach        foreach (string day in week)        {            System.Console.Write(day + " ");        }    }}