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):

  1. For Statement with cached count
    int iCount = list.Count;
    for (int i = 0; i < iCount; i++)
    {
    //do stuff
    }
  2. ForEach Delegate
    list.ForEach(delegate(ItemType item)
    {
    //do stuff
    }
    );
  3. ForEach Statement
    foreach (ItemType item in list)
    {
    //do stuff
    }

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.