using System.Collections.Generic;
private static readonly List<int> vals = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public static void Main()
var t = new System.Diagnostics.Stopwatch();
var total = RunningTotal();
Console.WriteLine(total);
long duration = t.ElapsedMilliseconds;
Console.WriteLine($"Normal run: {duration} ms");
t = new System.Diagnostics.Stopwatch();
foreach (int e in RunningTotalYield())
duration = t.ElapsedMilliseconds;
Console.WriteLine($"Yield run: {duration} ms");
private static int RunningTotal()
foreach (int val in vals)
private static IEnumerable<int> RunningTotalYield()
foreach (int val in vals)
yield return runningTotal;