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

368Part III Creating Components

5.Display the Program.cs file for the EnumeratorTest project in the Code and Text Editor window. Review the Main method in the Program.cs file. Recall from testing the earlier enumerator that this method instantiates a Tree<int> object, fills it with some data, and then uses a foreach statement to display its contents.

6.Build the solution, correcting any errors if necessary.

7.On the Debug menu, click Start Without Debugging.

The program runs and displays the values in the same sequence as before:

–12, –8, 0, 5, 5, 10, 10, 11, 14, 15

8.Press Enter and return to Visual Studio 2008.

If you want to continue to the next chapter

Keep Visual Studio 2008 running, and turn to Chapter 20. If you want to exit Visual Studio 2008 now

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Microsoft Visual C# 2008 Express Edition) and save the project.

Chapter 19 Quick Reference

To

Do this

Make a class enumerable,

allowing it to support the foreach construct

Implement the IEnumerable interface, and provide a GetEnumerator method that returns an IEnumerator object. For example:

public class Tree<TItem> : IEnumerable<TItem>

{

...

IEnumerator<TItem> GetEnumerator()

{

...

}

}

 

Chapter 19 Enumerating Collections

369

Implement an enumerator

Define an enumerator class that implements the IEnumerator

 

not by using an iterator

interface and that provides the Current property and the MoveNext

 

 

method (and optionally the Reset method). For example:

 

 

public class TreeEnumerator<TItem> : IEnumerator<TItem>

 

 

{

 

 

 

...

 

 

 

TItem Current

 

 

{

 

 

 

get

 

 

{

 

 

 

...

 

 

 

}

 

 

 

}

 

 

 

bool MoveNext()

 

 

{

 

 

 

...

 

 

 

}

 

 

 

}

 

 

Define an enumerator by

Implement the enumerator to indicate which items should be

 

using an iterator

returned (using the yield statement) and in which order. For example:

 

IEnumerator<TItem> GetEnumerator()

{

for (...)

yield return ...

}

Chapter 20

Querying In-Memory Data by Using Query Expressions

After completing this chapter, you will be able to:

Define Language Integrated Query (LINQ) queries to examine the contents of enumerable collections.

Use LINQ extension methods and query operators.

Explain how LINQ defers evaluation of a query and how you can force immediate execution and cache the results of a LINQ query.

You have now met most of the features of the C# language. However, we have glossed over one important aspect of the language that is likely to be used by many applications—the support that C# provides for querying data. You have seen that you can define structures and classes for modeling data and that you can use collections and arrays for temporarily storing data in memory. However, how do you perform common tasks such as searching for items

in a collection that match a specific set of criteria? For example, if you have a collection of Customer objects, how do you find all customers that are located in London, or how can you

find out which town has the most customers for your services? You can write your own code to iterate through a collection and examine the fields in each object, but these types of tasks occur so often that the designers of C# decided to include features to minimize the amount of code you need to write. In this chapter, you will learn how to use these advanced C# language features to query and manipulate data.

What Is Language Integrated Query (LINQ)?

All but the most trivial of applications need to process data. Historically, most applications provided their own logic for performing these operations. However, this strategy can lead to the code in an application becoming very tightly coupled to the structure of the data that it processes; if the data structures change, you might need to make a significant number of changes to the code that handles the data. The designers of the Microsoft .NET Framework thought long and hard about these issues and decided to make the life of an application developer easier by providing features that abstract the mechanism that an application uses to

query data from application code itself. These features are called Language Integrated Query, or LINQ.

371

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