foreach yeild code example
Example: c# yield
using System;
using System.Collections.Generic;
public class YieldSample
{
static void Main()
{
foreach (var number in GenerateWithoutYield())
Console.WriteLine(number);
}
public static IEnumerable<int> GenerateWithoutYield()
{
var i = 0;
var list = new List<int>();
while (i < 5) // yield gives the control to this part and will hit only this part everytime we call GenerateWithoutYield
yield return ++i;
}
}