C Sharp Syntax - Enumerators

Enumerators

An enumerator is an iterator. Enumerators are typically obtained by calling the GetEnumerator method of an object implementing the IEnumerable interface. Container classes typically implement this interface. However, the foreach statement in C# can operate on any object providing such a method, even if it doesn't implement IEnumerable. This interface was expanded into generic version in .NET 2.0.

The following shows a simple use of iterators in C# 2.0:

// explicit version IEnumerator iter = list.GetEnumerator; while (iter.MoveNext) Console.WriteLine(iter.Current); // implicit version foreach (MyType value in list) Console.WriteLine(value);

Read more about this topic:  C Sharp Syntax