Technology
C#: IEnumerable, yield return, and lazy evaluation
Let’s talk about one of my favorite .NET features.
IEnumerable is an interface for iterating over a collection. In other words, if something is an IEnumerable, you can mostly think of it like an Array or a List. You can use a foreach
statement to loop through it, you can use LINQ to map or reduce it in a hundred different ways, or you can explicitly cast it to an array with .ToArray()
and access elements by index. But there are a few things that make IEnumerables special–and a few things that make them tricky.
This article has been republished on The Overflow. Keep Reading