Tuesday, June 17, 2014

Iterator Enumerator with Yield Return.


MSDN :-
 
public System.Collections.IEnumerable SampleIterator(int start, int end)
{
    for (int i = start; i <= end; i++)
    {
        yield return i;
    }
}
We can fetch the results from SampleIterator:-
ListClass test = new ListClass();

foreach (int n in test.SampleIterator(1, 10))
{
    System.Console.Write(n + " ");
}
// Output: 1 2 3 4 5 6 7 8 9 10


No comments :