Along with the methods already proposed for using the foreach , I thought that I also mention that any object that implements IEnumerable also provides an IEnumerator interface using the GetEnumerator method. Although this method is generally not needed, it can be used to manually iterate over collections and is especially useful when writing custom extension methods for collections.
IEnumerable<T> mySequence; using (var sequenceEnum = mySequence.GetEnumerator()) { while (sequenceEnum.MoveNext()) {
A prime example is that you want to iterate over two sequences simultaneously, which is not possible in a foreach .
Noldorin Oct 07 '09 at 16:54 2009-10-07 16:54
source share