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

CHAPTER 6 MORE ABOUT CLASSES

The Indexer get Accessor

When the indexer is used to retrieve a value, the get accessor is called with one or more index parameters. The index parameters represent which value to retrieve.

string s = emp[0];

Index parameter

The code in the get accessor body must examine the index parameters, determine which field they represent, and return the value of that field.

Figure 6-19 shows the syntax and meaning of the get 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 semantics of the get accessor are as follows:

It has the same parameter list as in the indexer declaration.

It returns a value of the same type as the indexer.

Figure 6-19. The syntax and meaning of the get accessor declaration

More About Indexers

As with properties, the get and set accessors cannot be called explicitly. Instead, the get accessor is called automatically when the indexer is used in an expression for a value. The set accessor is called automatically when the indexer is assigned a value with the assignment statement.

When an indexer is “called,” the parameters are supplied between the square brackets.

Index Value

emp[0]

= "Doe";

//

Calls

set

accessor

string

NewName = emp[0];

//

Calls

get

accessor

 

 

 

 

 

 

Index

 

 

 

 

Declaring the Indexer for the Employee Example

The following code declares an indexer for the earlier example: class Employee.

152

CHAPTER 6 MORE ABOUT CLASSES

The indexer must read and write values of type string—so string must be declared as the indexer’s type. It must be declared public so that it can be accessed from outside the class.

The three fields in the example have been arbitrarily indexed as integers 0 through 2, so the formal parameter between the square brackets, named index in this case, must be of type int.

In the body of the set accessor, the code determines which field the index refers to and assigns the value of implicit variable value to it. In the body of the get accessor, the code determines which field the index refers to and returns that field’s value.

class Employee

 

 

{

 

 

public string

LastName;

// Call this field 0.

public string

FirstName;

// Call this field 1.

public string

CityOfBirth;

// Call this field 2.

public string

this[int index]

// Indexer declaration

{

 

 

set

 

// Set accessor declaration

{

 

 

switch (index)

 

{

 

 

case

0: LastName = value;

 

break;

 

case

1: FirstName = value;

 

break;

 

case

2: CityOfBirth = value;

 

break;

 

default:

// (Exceptions in Ch. 11)

throw new ArgumentOutOfRangeException("index");

}

 

 

}

 

 

get

 

// Get accessor declaration

{

 

 

switch (index)

 

{

 

 

case

0: return LastName;

 

case

1: return FirstName;

 

case

2: return CityOfBirth;

 

default:

// (Exceptions in Ch. 11)

throw new ArgumentOutOfRangeException("index");

}

}

}

}

153

CHAPTER 6 MORE ABOUT CLASSES

Another Indexer Example

The following is an additional example that indexes the two int fields of class Class1:

class Class1

 

{

 

int Temp0;

// Private field

int Temp1;

// Private field

public int this [ int index ]

// The indexer

{

 

get

 

{

 

return ( 0 == index )

// Return value of either Temp0 or Temp1

? Temp0

 

: Temp1;

 

}

 

set

 

{

 

if( 0 == index )

 

Temp0 = value;

// Note the implicit variable "value".

else

 

Temp1 = value;

// Note the implicit variable "value".

}

 

}

 

}

 

class Example

 

{

 

static void Main()

 

{

 

Class1 a = new Class1();

 

Console.WriteLine("Values -- T0: {0}, T1: {1}", a[0], a[1]); a[0] = 15;

a[1] = 20;

Console.WriteLine("Values -- T0: {0}, T1: {1}", a[0], a[1]);

}

}

This code produces the following output:

Values -- T0: 0, T1: 0

Values -- T0: 15, T1: 20

154

CHAPTER 6 MORE ABOUT CLASSES

Indexer Overloading

A class can have any number of indexers, as long as the parameter lists are different; it isn’t sufficient for the indexer type to be different. This is called indexer overloading, because all the indexers have the same “name”—the this access reference.

For example, the following class has three indexers: two of type string and one of type int. Of the two indexers of type string, one has a single int parameter, and the other has two int parameters.

class MyClass

{

public string this [ int index ]

{

get { ... } set { ... }

}

public string this [ int index1, int index2 ]

{

get { ... } set { ... }

}

public int this [ float index1 ]

{

get { ... } set { ... }

}

...

}

Note Remember that the overloaded indexers of a class must have different parameter lists.

155

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