BlackWaspTM
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

Syntax

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' code block:

int _lowerBound;
int _upperBound;
string[] _items;
16 January 2008