Iterator

Why Trust Techopedia

What Does Iterator Mean?

An iterator, in the context of C#, is a block of code that returns an ordered sequence of values of a collection or array. It is a member function implemented using the iterator block, which contains one or more statements containing the "yield" keyword.

Advertisements

An iterator is used to enable consumers of a container class, containing a collection or array, to traverse the collection using the "foreach" statement in a simpler manner. It is used with strongly typed collection classes to iterate complex data structures like binary trees, which require recursive traversal and maintain the iteration state through the recursion. The concept of iterator is also used in implementing deferred execution in LINQ queries.

Techopedia Explains Iterator

The iterator is based on a design pattern that provides a method to traverse the elements of a collection of items sequentially without exposing the underlying structure of the collection. It eliminates the tedious task of implementing the "IEnumerator" interface manually when creating collection classes that support the "foreach" statement. The Intermediate Language code necessary for implementing the methods and properties of the IEnumerator interface is generated by the compiler, which results in simpler syntax, reduced code size, and improvement in developer productivity.

In general, an iterator is similar to a database cursor in that it provides access to data elements in a collection, but does not perform iteration. An iterator can be implemented in C# as a method, operator, or get accessor. For example, an iterator can be used to traverse a collection of strings to display the content of each string in the collection.

An iterator is represented by the IEnumerator interface and is implemented by the compiler with the following methods:

  • MoveNext: A method that advances to the next element of the collection and indicates the end of that collection
  • Current: A property that fetches the value of the element currently being pointed to
  • Dispose: Cleans up the iteration

GetEnumerator() is the default iterator method of the IEnumerable interface. This method can be implemented in the collection that contains the container class. The GetEnumerator() method is invoked on execution of the "foreach" statement, which uses the returned enumerator to iterate through values. The "yield return" statement is used within the iterator block to fetch an element of the collection. It also helps to position the current location such that execution starts from this location the next time it occurs. The "yield break" statement ends the iteration.

Advertisements

Related Terms

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…