Using Iterator Blocks (C#)

Cross-posted from Ryan's blog. Iterator blocks (more broadly called generators) are a special syntax for producing an enumerator. They allow fine-grained control over iteration, including: Controlling the order of iteration Modifying a collection during iteration Loading a large collection in batches Creating the collection as it is consumed Implementing infinite…

What does foreach really do? (.NET)

Cross-posted from Ryan's blog. In this post I’ll explain how the C# compiler interprets the foreach keyword. I’ll start by looking at the common pattern of enumerating a list. var numbers = new List<int>() { 1, 2, 3, 4, 5 }; foreach(var n in numbers) { Console.WriteLine(…