Information Technology Reference
In-Depth Information
An Additional Example of an Indexer
The following is an additional example—indexing 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
Search WWH ::




Custom Search