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(…