Yield to the iterator
Posted by Chris Alcock on Wednesday 24th January 2007 at 11:59 pm | Tagged as: .NET, C#, Design, Development
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++)
{
//do stuff
}
-
ForEach Delegate
list.ForEach(delegate(ItemType item)
{
//do stuff
}
);
- 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.
Comments Off on Yield to the iterator