BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Indexers

The thirteenth part of the C# Object-Oriented Programming tutorial describes the use of indexers. A class that has an indexer can be used in a similar manner to an array. Objects of the class can use array-style notation to present multiple values.

Testing the MyArray Class

The new class can be tested using the Main method of the program. Open the Program class and add the following code to create a new instance of MyArray and to populate it with values.

static void Main(string[] args)
{
    MyArray fruit = new MyArray(-2, 1);
    fruit[-2] = "Apple";
    fruit[-1] = "Orange";
    fruit[0] = "Banana";
    fruit[1] = "Blackcurrant";
    Console.WriteLine(fruit[-1]);       // Outputs "Orange"
    Console.WriteLine(fruit[0]);        // Outputs "Banana"
}

Creating a Multidimensional Indexer

Syntax

Indexers are not limited to a single dimension. By including more than one index variable in the square brackets of the indexer declaration, multiple dimensions may be added. For example, to declare a two-dimensional indexer the syntax is as follows:

public data-type this[index-type1 index-name1, index-type2 index-name2]
{
    get {}
    set {}
}

It is also possible to overload indexers is a similar manner to overloading methods. Several declarations for this[] can be included each with a different set of parameters, or signature. We can demonstrate this by adding a second indexer to the MyArray class. This time the indexer will have two integer parameters. The first will determine which item from the private array will be looked up. The second parameter will determine a position within the string and return the character at that position. For simplicity the following code creates an overloaded read-only indexer.

public string this[int word, int position]
{
    get { return _items[word - _lowerBound].Substring(position, 1); }
}

To test the second indexer, modify the Main method as follows:

static void Main(string[] args)
{
    MyArray fruit = new MyArray(-2, 1);
    fruit[-2] = "Apple";
    fruit[-1] = "Orange";
    fruit[0] = "Banana";
    fruit[1] = "Blackcurrant";
    Console.WriteLine(fruit[-1, 0]);    // Outputs "O"
    Console.WriteLine(fruit[0, 2]);     // Outputs "n"
}

Creating an Indexer with no Underlying Array

As mentioned at the beginning of this article, there is no requirement that the indexer is linked to an underlying array or collection. Sometimes it is useful to use the indexer to refer to a row from a database table, a position within a text file or XML document or simply to perform a calculation.

The following indexer, added to the MyArray class, uses no lookup at all. Instead, the floating-point value passed to the indexer is simply squared:

public float this[float toSquare]
{
    get { return toSquare * toSquare; }
}

This can be tested with a final change to the Main method:

static void Main(string[] args)
{
    MyArray fruit = new MyArray(0, 0);
    Console.WriteLine(fruit[5F]);       // Outputs 25
}
16 January 2008