logo

C# Corner - fun with yield and extension methods

All .NET developers learn about IEnumerable and IEnumerator in their first day of development, whether they realize it or not. Every foreach statement written utilizes the .GetEnumerator() call to iterate over a collection of objects. The “For Each” concept is a really nice piece of syntactic sugar. However, one of its limitations is that it doesn’t give you a counter to track which loop iteration you’re on. If you want to do that, you have to keep track of it yourself in your own variable.
4 minutes to read