I had planned to write a piece about using yield return in c# to allow returning IEnumerable collections in a lazy way, however upon starting up Visual Studio in the news I discovered Bill Wagner’s piece on MSDN entitled ‘Custom Iterators‘ which covers everything I was planning to write about.
On a related note, I happened across a rather old post about the performance of iterating over generic lists – the article compares three ways of iterating the list, with the outcome being (fastest to slowest):
- For Statement with cached count
int iCount = list.Count;
for (int i = 0; i < iCount; i++)
- ForEach Statement
foreach (ItemType item in list)
This ordering was pretty much how I expected it to be, but the article has fully example code and some figures to back up the performance difference.
Comments Off on Yield to the iterator