nerdexam
Microsoft

98-372 · Question #94

You are creating an application using .NET Framework 4.0. You need to use an iterator in the application. What is the purpose of using an iterator in a C# application?

The correct answer is D. It simplifies the implementation of the IEnumerable interface. An iterator in C# simplifies the implementation of the IEnumerable interface. When the compiler detects an iterator, it will automatically generate the Current, MoveNext, and Dispose methods of the IEnumerable or IEnumerable interface. An iterator is an object that allows a…

Understanding Control Flow

Question

You are creating an application using .NET Framework 4.0. You need to use an iterator in the application. What is the purpose of using an iterator in a C# application?

Options

  • AIt improves the performance of value types.
  • BIt is the only way for a class to be compatible with a for-each loop.
  • CIt enables an array to be sorted.
  • DIt simplifies the implementation of the IEnumerable interface.

How the community answered

(23 responses)
  • A
    9% (2)
  • B
    17% (4)
  • C
    4% (1)
  • D
    70% (16)

Explanation

An iterator in C# simplifies the implementation of the IEnumerable interface. When the compiler detects an iterator, it will automatically generate the Current, MoveNext, and Dispose methods of the IEnumerable or IEnumerable interface. An iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. An iterator may be thought of as a type of pointer which has two primary operations: referencing one particular element in the object collection and modifying itself so it points to the next element. 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. This allows the container to store elements in any manner it wishes while allowing the user to treat it as if it were a simple sequence or list. An iterator class is usually designed in tight coordination with the corresponding container class. Usually the container provides the methods for creating iterators.

Topics

#iterators#IEnumerable#foreach loop#C# collections

Community Discussion

No community discussion yet for this question.

Full 98-372 Practice