Iterator

Iterator

An Iterator is a special type of object that provides the means to iterate over ‘container’ objects, containing a collection of elements of the same type. Each container type provides a GetIterator() method that returns an instance of the appropriate iterator type pointing to the beginning of the container. Iterators can be used directly to iterate over a range of elements in the container, but the foreach loop is simpler when iterating over the entire container.

Methods

bool HasNext()

Returns true if another element exists after the current element.

Object Next()

Increments the iterator to the next element and returns its associated data, which should be cast to the data type of the container before usage. If at the end of the container and no next element exists, invalid data is returned, so HasNext() should be queried before using this method.

See specialized iterator classes for a list of additional methods available for iterating over specific container types: