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

CHAPTER 6 MORE ABOUT CLASSES

Indexers

Suppose you were to define class Employee, with three fields of type string (as shown in Figure 6-14). You could then access the fields using their names, as shown in the code in Main.

Figure 6-14. Simple class without indexers

There are times, however, when it would be convenient to be able to access them with an index, as if the instance were an array of fields. This is exactly what indexers allow you to do. If you were to write an indexer for class Employee, method Main might look like the code in Figure 6-15. Notice that instead of using dot-syntax notation, indexers use index notation, which consists of an index between square brackets.

Figure 6-15. Using indexed fields

148

CHAPTER 6 MORE ABOUT CLASSES

What Is an Indexer?

An indexer is a pair of get and set accessors, similar to those of properties. Figure 6-16 shows the representation of an indexer for a class that can get and set values of type string.

Figure 6-16. Representations of an indexer

Indexers and Properties

Indexers and properties are similar in many ways.

Like a property, an indexer does not allocate memory for storage.

Both indexers and properties are used primarily for giving access to other data members with which they’re associated and for which they provide get and set access.

A property usually represents a single data member.

An indexer usually represents multiple data members.

Note You can think of an indexer as a property that gives get and set access to multiple data members of the class. You select which of the many possible data members by supplying an index, which itself can be of any type—not just numeric.

Some additional points you should know when working with indexers are the following:

Like a property, an indexer can have either one or both of the accessors.

Indexers are always instance members; hence, an indexer cannot be declared static.

Like properties, the code implementing the get and set accessors does not have to be associated with any fields or properties. The code can do anything, or nothing, as long as the get accessor returns some value of the specified type.

149

CHAPTER 6 MORE ABOUT CLASSES

Declaring an Indexer

The syntax for declaring an indexer is shown next. Notice the following about indexers:

An indexer does not have a name. In place of the name is the keyword this.

The parameter list is between square brackets.

There must be at least one parameter declaration in the parameter list.

 

Keyword

Parameter list

 

 

 

 

ReturnType this [ Type param1, ... ]

{

 

get

Square bracket

Square bracket

{

...

}

set

{

...

}

}

Declaring an indexer is similar to declaring a property. Figure 6-17 shows the syntactic similarities and differences.

Figure 6-17. Comparing an indexer declaration to a property declaration

150

CHAPTER 6 MORE ABOUT CLASSES

The Indexer set Accessor

When the indexer is the target of an assignment, the set accessor is called and receives two items of data, as follows:

An implicit parameter, named value, which holds the data to be stored

One or more index parameters that represent where it should be stored

emp[0] = "Doe";

↑ ↑

Index Value Parameter

Your code in the set accessor must examine the index parameters, determine where the data should be stored, and then store it.

Figure 6-18 shows the syntax and meaning of the set accessor. The left side of the figure shows the actual syntax of the accessor declaration. The right side shows the semantics of the accessor if it were written using the syntax of a normal method. The figure on the right shows that the set accessor has the following semantics:

It has a void return type.

It uses the same parameter list as that in the indexer declaration.

It has an implicit value parameter named value, of the same type as the indexer.

Figure 6-18. The syntax and meaning of the set accessor declaration

151

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