C# Programming Guide
Indexers (C# Programming Guide)

Indexers permit instances of a class or struct to be indexed in the same way as arrays. Indexers are similar to properties except that their accessors take parameters.

In the following example, a generic class is defined and provided with simple get and set accessor methods as a means for assigning and retrieving values. The class Program creates an instance of this class for storing strings.

C#
class SampleCollection<T>
{
    private T[] arr = new T[100];
    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer
class Program
{
    static void Main(string[] args)
    {
        SampleCollection<string> stringCollection = new SampleCollection<string>();
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
Indexers Overview

  • Indexers enable objects to be indexed in a similar way to arrays.

  • A get accessor returns a value. A set accessor assigns a value.

  • The this keyword is used to define the indexers.

  • The value keyword is used to define the value being assigned by the set indexer.

  • Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.

  • Indexers can be overloaded.

  • Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.

Related Sections

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6.6.3 Indexers

  • 10.2.7.3 Member names reserved for indexers

  • 10.8 Indexers

  • 13.2.4 Interface indexers

See Also

Reference

Properties (C# Programming Guide)

Concepts

C# Programming Guide

Tags :


Community Content

LukeSkywalker
The Indexed Value Replacement Paradox

How are people dealing with set accessors on custom indexes with respect to overwriting values? For example, you have a customer collection of items that have Guids. You decide that an indexer this[someGuid] is a useful thing. But look at this example:

CustomerCollection customers = new CustomerCollection();
Customer cust1 = new Customer(); // internally, the ctor assigns customer a guid.
Guid g = cust1.Guid; // cache the guid in g.
customers.Add(cust1); // add our cust1 to the collection.
Customer c = customers[g]; // retrieving an object via the indexer is fine.
customers[g] = new Customer(); // but here's the dichotomy; the cust at g will be replaced by a cust with a new guid.

Depending upon the implementation of the indexer's set accessor, in that last line, the cust1 object could either be replaced by the new incoming customer (that would have a new guid), or it could be written so that the set accessor would only allow replacements by objects that have the same guid.

Or you could think of it this way;

son2.Name = "David"; sonsCollection["Mark"] = son2;
Son randomSon = sonsCollection["Mark"]; //runtime error - Mark's gone!

So which is the programmer's morally right way? We should code in the way which is the expected behaviour. Which is it? Personally, I vote for overwriting/replacing the value at the index, even if it does invalidate the index at which you're writing to.

Tags :

Page view tracker