November 11, 2008

ForEach Implementation

This code shows in a simplistic manner how foreach is implemented using iterators. See the official explanantion or here for more details
// foreach Implementation
IEnumerable enumerable = (IEnumerable)targObject;
IEnumerator it = enumerable.GetEnumerator();
//it.Reset() NOT actuall called underneath.
while(it.MoveNext())
{
    DoSomething(it.Current);
}

No comments: