Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

Chapter 19 Enumerating Collections

361

Initializing a Variable Defined with a Type Parameter

You should have noticed that the statement that defines and initializes the currentItem variable uses the default keyword. The currentItem variable is defined by using the type parameter TItem. When the program is written and compiled, the actual type that will

be substituted for TItem might not be known—this issue is resolved only when the

code is executed. This makes it difficult to specify how the variable should be initialized. The temptation is to set it to null. However, if the type substituted for TItem is a value

type, this is an illegal assignment. (You cannot set value types to null, only reference types.) Similarly, if you set it to 0 in the expectation that the type will be numeric, this

will be illegal if the type used is actually a reference type. There are other possibilities as well—TItem could be a boolean, for example. The default keyword solves this prob-

lem. The value used to initialize the variable will be determined when the statement is executed; if TItem is a reference type, default(TItem) returns null; if TItem is numeric, default(TItem) returns 0; if TItem is a boolean, default(TItem) returns false. If TItem is a struct, the individual fields in the struct are initialized in the same way (reference fields are set to null, numeric fields are set to 0, and boolean fields are set to false).

Implementing the IEnumerable Interface

In the following exercise, you will modify the binary tree class to implement the IEnumerable interface. The GetEnumerator method will return a TreeEnumerator<TItem> object.

Implement the IEnumerable<TItem> interface in the Tree<TItem> class

1.In Solution Explorer, double-click the file Tree.cs to display the Tree<TItem> class in the

Code and Text Editor window.

2.Modify the definition of the Tree<TItem> class so that it implements the IEnumerable<TItem> interface, as shown in bold type in the following code:

public class Tree<TItem> : IEnumerable<TItem> where TItem : IComparable<TItem>

Notice that constraints are always placed at the end of the class definition.

3.Right-click the IEnumerable<TItem> interface in the class definition, point to Implement Interface, and then click Implement Interface Explicitly.

This action generates implementations of the IEnumerable<TItem>.GetEnumerator and IEnumerable.GetEnumerator methods and adds them to the class. The nongeneric IEnumerable interface method is implemented because the generic

IEnumerable<TItem> interface inherits from IEnumerable.

362Part III Creating Components

4.Locate the generic IEnumerable<TItem>.GetEnumerator method near the end of the class. Modify the body of the GetEnumerator() method, replacing the existing throw statement as shown in bold type here:

IEnumerator<TItem> IEnumerable<TItem>.GetEnumerator()

{

return new TreeEnumerator<TItem>(this);

}

The purpose of the GetEnumerator method is to construct an enumerator object

for iterating through the collection. In this case, all you need to do is build a new TreeEnumerator<TItem> object by using the data in the tree.

5.Build the solution.

The project should compile cleanly, but correct any errors that are reported and rebuild the solution if necessary.

You will now test the modified Tree<TItem> class by using a foreach statement to iterate through a binary tree and display its contents.

Test the enumerator

1.In Solution Explorer, right-click the BinaryTree solution, point to Add, and then click New Project. Add a new project by using the Console Application template. Name the project EnumeratorTest, set the Location to \Microsoft Press\Visual CSharp Step By Step\ Chapter 19 in your Documents folder, and then click OK.

2.Right-click the EnumeratorTest project in Solution Explorer, and then click Set as Startup Project.

3.On the Project menu, click Add Reference. In the Add Reference dialog box, click the Projects tab. Select the BinaryTree project, and then click OK.

The BinaryTree assembly appears in the list of references for the EnumeratorTest project in Solution Explorer.

4.In the Code and Text Editor window displaying the Program class, add the following using directive to the list at the top of the file:

using BinaryTree;

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