An iterator is a C# language feature that helps in writing collections, in the same way the foreach statement helps in consuming collections. An iterator automatically handles the implementation of IEnumerable and IEnumerator or their generic versions. The yield return statement causes an element in the source sequence to be returned immediately to the caller before the next element in the source sequence is accessed. Although you write an iterator as a method, the compiler actually translates it into a nested class that is, in effect, a state machine. This class keeps track of the position of the iterator as long the foreach loop on the client code continues. Among other languages, iterators are used in C++, C# and other .NET languages, Java, Ruby, and Python. The primary purpose of an iterator is to allow a user to process every element of a container while isolating the user from the internal structure of the container. Here is an example: public class MyCollection : IEnumerable { int[] data = { 1, 2, 3 }; public IEnumerator GetEnumerator() { foreach (int i in data) yield return i; } } Notice that GetEnumerator doesn’t seem to return an enumerator at all. When parsing the yield return statement, the compiler writes a hidden nested enumerator class behind the scenes, and then refactors GetEnumerator to instantiate and return that class. Iterators are powerful and simple, and they are the basis for much of the implementation of LINQ. Using this approach, you can also implement the generic interface IEnumerable<T>: public class MyGenericCollection : IEnumerable<int> { int[] data = { 1, 2, 3 }; public IEnumerator<int> GetEnumerator() { foreach (int i in data) yield return i; } IEnumerator IEnumerable.GetEnumerator() // Explicit implementation is hidden { return GetEnumerator(); } } Because IEnumerable<T> implements IEnumerable, you have to implement both the generic and the nongeneric versions of GetEnumerator. In accordance with standard practice, the nongeneric version is implemented explicitly. It can simply call the generic GetEnumerator because IEnumerator<T> implements nongeneric IEnumerator. The class above would be suitable as a basis from which to write a more sophisticated collection. But if you need no more than a simple IEnumerable<T> implementation, the yield return statement makes it easier. Instead of writing a class, you can move the iteration logic into a method returning a generic IEnumerable<T> and let the compiler take care of the rest. An example: public class Test { public static IEnumerable <int> GetSomeIntegers() { yield return 1; yield return 2; yield return 3; } } This is the method in action: foreach (int i in Test.GetSomeIntegers()) Console.WriteLine (i); Output: 1 2 3 Iterators Overview: