Can somebody provide a real life example regarding use of iterators. I tried searching google but was not satisfied with the answers.
Simple example : a function that generates a sequence of integers :
static IEnumerable GetSequence(int fromValue, int toValue)
{
if (toValue >= fromValue)
{
for (int i = fromValue; i <= toValue; i++)
{
yield return i;
}
}
else
{
for (int i = fromValue; i >= toValue; i--)
{
yield return i;
}
}
}
To do it without an iterator, you would need to create an array then enumerate it...