Consider this code:
I would expect after the using statement that dispose of Test is called when calling Bla(). But it isn'tCode:class Test : IDisposable { public void Dispose() { Console.WriteLine("dispose"); } } static IEnumerable<int> Bla() { using (new Test()) { } yield return 1; yield return 2; yield return 3; }
Dispose is only called when calling Bla in a loop.
Why?Code:foreach (int i in Bla()) {break;}