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
Technology Expert
Margaret Rouse
Technology Expert

Margaret é uma premiada redatora e professora conhecida por sua habilidade de explicar assuntos técnicos complexos para um público empresarial não técnico. Nos últimos vinte anos, suas definições de TI foram publicadas pela Que em uma enciclopédia de termos tecnológicos e citadas em artigos do New York Times, Time Magazine, USA Today, ZDNet, PC Magazine e Discovery Magazine. Ela ingressou na Techopedia em 2011. A ideia de Margaret de um dia divertido é ajudar os profissionais de TI e de negócios a aprenderem a falar os idiomas altamente especializados uns dos outros.