Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 20 ENUMERATORS AND ITERATORS

Enumerators and Enumerable Types

In Chapter 14, you saw that you can use a foreach statement to cycle through the elements of an array. In this chapter, you’ll take a closer look at arrays and see why they can be processed by foreach statements. You’ll also look at how you can add this capability to your own user-defined classes. Later in the chapter, I’ll explain the use of iterators.

Using the foreach Statement

When you use a foreach statement with an array, the statement presents you with each element in the array, one by one, allowing you to read its value.

For example, the following code declares an array with four elements and then uses a foreach loop to print out the values of the items:

int[] arr1 =

{ 10, 11, 12,

13 };

// Define the array.

foreach (int

item in arr1)

 

// Enumerate the elements.

Console.WriteLine("Item

value:

{0}", item);

This code produces the following output:

 

 

 

 

 

 

Item value:

10

 

 

 

 

Item value:

11

 

 

 

 

Item value:

12

 

 

 

 

Item value:

13

 

 

 

 

 

 

 

 

 

 

Why does this work, apparently magically, with arrays? The reason is that an array can produce, upon request, an object called an enumerator. The enumerator is an object that can return the elements of the array, one by one, in order, as they are requested. The enumerator “knows” the order of the items and keeps track of where it is in the sequence. It then returns the current item when it is requested.

For types that have enumerators, there must be a way of retrieving them. The standard way of retrieving an object’s enumerator in .NET is to call the object’s GetEnumerator method. Types that implement a GetEnumerator method are called enumerable types, or just enumerables. Arrays are enumerables.

Figure 20-1 illustrates the relationship between enumerables and enumerators.

506

CHAPTER 20 ENUMERATORS AND ITERATORS

Figure 20-1. Overview of enumerators and enumerables

The foreach construct is designed to work with enumerables. As long as the object it is given to iterate over is an enumerable type, such as an array, it will perform the following actions:

Get the object’s enumerator by calling its GetEnumerator method

Request each item from the enumerator and make it available to your code as the iteration variable, which your code can read (but not change).

Must be enumerable

foreach( Type VarName in EnumerableObject )

{

...

}

Types of Enumerators

There are three variations on enumerators. They all work essentially the same way, with only slight differences. I'll discuss all three types. You can implement enumerators using the following:

The IEnumerator/IEnumerable interfaces—called the nongeneric interface form

The IEnumerator<T>/IEnumerable<T> interfaces—called the generic interface form

The form that uses no interfaces

507

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]