2 out of 5 rated this helpful Rate this topic

Indexers (C# Programming Guide)

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble 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 of assigning and retrieving values. The Program class creates an instance of this class for storing strings.


class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)        
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}


  • Indexers enable objects to be indexed in a similar manner 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.

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
indexer and interfaces
Hi.
I want to know the use of interface while using indexers along with a class ,that implements interface.
Please explain this concept with simple example

[tfl - 10 10 10] Hi - and thanks for your post.You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn. You are much more likely get a quick response 

using the forums than through the Community Content.

For specific help about:
.Net Development : http://social.msdn.microsoft.com/Forums/en-US/category/visualstudio
Open Specifications : http://social.msdn.microsoft.com/Forums/en-US/category/openspecifications
SharePoint 2010 : http://social.msdn.microsoft.com/Forums/en-US/category/sharepoint2010
SQL : http://social.msdn.microsoft.com/Forums/en-US/category/sqlserver
Visual Studio : http://social.msdn.microsoft.com/Forums/en-US/category/visualstudio


Edit by SJ at MSFT: Also, there is a topic just under this one in the table of contents called "Indexers in Interfaces," http://msdn.microsoft.com/en-us/library/tkyhsw31.aspx.
Using indexers in interfaces
There isn't much difference in using indexers in interfaces, except for a potential of needing to account for multiple implementations with signature matched interfaces.  See:  http://msdn.microsoft.com/en-us/library/tkyhsw31.aspx

--Serp
Advertisement