What is an iterator? 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. Example: publicclassYieldTest... {publicstaticIEnumertablePower (intnumber, intexponent)... {intcount
What is an iterator? 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. Example: public class YieldTest... {public static IEnumertablePower (int number, int exponent)... {int count
What is an iterator?
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.
Example:
Public class YieldTest
...{
Public static IEnumertable Power (int number, int exponent)
...{
Int counter = 0;
Int result = 1;
While (counter ++ <exponent)
...{
Result = result * number
Yield return result
}
}
}
1. The iterator is a piece of code that can return an ordered sequence of values of the same type.
2. The iterator can be used as a method, operator, or get accessors code body.
3. The iterator Code uses the yield return statement to return each element at a time.
4. yield break will terminate Iteration
5. You can implement an iterator in the class. Each iterator must have a unique name like any class member and can be called in a foreach statement.
6. The return type of the iterator must be IEnumerable, IEnumberator, and IEnumerbale. Or IEnumberator
Foreach (int I in YieldTest. Power)
{
}