
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Adding Multiple Iterators on a Single Type
|
341
Discussion
The SimpleListIterator class contains the typical GetEnumerator iterator method that
yields all items in the
_items list. In addition to this iterator method, the class con-
tains three additional iterators: ReverseOrder, FirstHalf, and SecondHalf. The
ReverseOrder iterator simply yields the elements in the _items list in reverse-index
order, whereas the
GetEnumerator method yields all elements in forward-index order.
This is accomplished by setting up a
for loop to start at either the zeroth element, for
the
GetEnumerator iterator method, or at the last element, as with the ReverseOrder
iterator property.
The
FirstHalf iterator property starts at the zeroth index of the _items list and yields
all elements in the list up to the middle index. At this point iteration stops. The
SecondHalf iterator property starts where the FirstHalf iterator property left off and
continues yielding elements of the
_items list until the end of this list.
The following code shows how the
GetEnumerator iterator method is used, as well as
the three iterator properties:
public static void TestIteratorProperties( )
{
//Create SimpleListIterator object and fill it with data.
SimpleListIterator b = new SimpleListIterator( );
b.AddItem("item1");
b.AddItem("item2"); ...