using System.Diagnostics;
using System.Collections;
private const int Iterations = 190000;
public static void Main()
var sw = new Stopwatch();
IEnumerator enumerator1 = YieldBreak();
for (int i = 0; i < Iterations; i++)
while(enumerator1.MoveNext())
throw new InvalidOperationException("Should not occur");
Console.WriteLine("Yield break: {0}", sw.Elapsed);
IEnumerator enumerator2 = Enumerable.Empty<object>().GetEnumerator();
for (int i = 0; i < Iterations; i++)
while(enumerator2.MoveNext())
throw new InvalidOperationException("Should not occur");
Console.WriteLine("Enumerable.Empty<T>(): {0}", sw.Elapsed);
var enumerator3 = new EmptyEnumerator();
for (int i = 0; i < Iterations; i++)
while(enumerator3.MoveNext())
throw new InvalidOperationException("Should not occur");
Console.WriteLine("EmptyEnumerator instance: {0}", sw.Elapsed);
public static IEnumerator YieldBreak()
private class EmptyEnumerator : IEnumerator
public object Current { get { return null; } }