When i have a code block
static void Main()
{
foreach (int i in YieldDemo.SupplyIntegers())
{
Console.WriteLine("{0} is consumed by foreach iteration", i);
}
}
class YieldDemo
{
public static IEnumerable<int> SupplyIntegers()
{
yield return 1;
yield return 2;
yield return 3;
}
}
can i interpret the principle behind yield return as
- Main() calls the SupplyIntegers()
|1| |2| |3| are stored in contiguous memory block.Pointer of "IEnumerator" Moves to |1|
- Control returns from SupplyInteger() to Main().
- Main() prints the value
- Pointer Moves to |2|, and so on.
Clarifications :
(1) Normally we will have one valid return statement is allowed inside a function.How does C# treats when multiple yield return ,yield return,... statements are present?
(2) Once the return is encountered there is no way for control again coming back to SupplyIntegers(), in case it is allowed won't the Yield again starts from 1 ? I mean yield return 1?