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.

What is an Indexer?

Most C# programmers first use an indexer when working with an array. An array is used to store a number of similar, related variables under the same name, with each variable accessed using an index number provided in square brackets. For example:

thirdItem = items[3];

Often it is useful to incorporate the square bracket notation for new classes. This may be because the class is used to store related information in a similar manner to an array, or simply because the index number can be useful in a calculation or lookup. Adding an indexer to a class provides this functionality.

Creating an Indexer


The simplest version of an indexer is the one-dimensional type. A one-dimensional indexer accepts a single value between the square brackets when used. The standard syntax used to declare the indexer is similar to that used to define the get and set accessors of a property. However, instead of defining a property name, the accessors are declared for this[] as follows:

public data-type this[index-type index-name]
    get {}
    set {}

In the syntax definition, data-type determines the type of information that will be returned when the indexer is queried and the type that will be required when setting a value. Index-type specifies the data type of the indexer itself. This permits declaration of indexers that are not based upon integer values, allowing similar functionality to that of a Hashtable for example. The index-name is the variable containing the index value that can be used during processing of the get and set accessors.

The get accessor is required for an indexer and must return a value of the type data-type. The set accessor is defined for writeable indexers and is omitted if a read-only variant is desired.

Creating a New Array-Like Class

To demonstrate the use of an indexer, in this article we will create a new class that behaves like a simple array of string variables. Unlike a standard array that only permits zero-based indexing, the new class will provide an integer-based array for which the programmer can specify the upper and lower boundaries using a constructor during instantiation.

To start, create a new console application named "IndexerDemo" and add a new class file named "MyArray".

Adding the Class Variables

The new array-like class requires three private variables. Two integer values will hold the upper and lower boundaries. An array of strings will also be required to store the items added to the MyArray class. This will be a zero-based array with the same length as the created MyArray object. The indexer will interpret the index number supplied and map it against this underlying array for get and set operations.

To add the class level variables, add the following code to the MyArray class's code block:

int _lowerBound;
int _upperBound;
string[] _items;

Adding the Constructor

The constructor for the new class will accept two integer parameters that define the upper and lower boundaries. These values will be stored in the two associated class variables. Using these boundaries the length of the underlying array can be calculated and the _items array can be initialised accordingly.

To create the constructor, add the following code to the class:

public MyArray(int lowerBound, int upperBound)
    _lowerBound = lowerBound;
    _upperBound = upperBound;
    _items = new string[1 + upperBound - lowerBound];

NB: To simplify this example validation checks have been omitted. In a real program you would want to validate that the upper boundary is larger than the lower boundary. Other validation checks in the code below have also been removed for clarity.

Adding the Indexer

Now that the preparation work is complete we can add the indexer to the class. For this simple array-like class the indexer accepts a single integer parameter containing the index of the string that is being read from or written to. This index needs to be adjusted to correctly map to the underlying data before returning the value from the array or writing the new value into the array.

The code to add the indexer is shown below. Note that, as with property declarations, the set accessor uses the 'value' variable to determine the value that has been assigned by the calling function:

public string this[int index]
    get { return _items[index - _lowerBound]; }
    set { _items[index - _lowerBound] = value; }
16 January 2008