NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Indexer Declaration

Indexers allow you to index a class or a struct instance in the same way as an array. To declare an indexer, use the following declaration:

[attributes] [modifiers] indexer-declarator {accessor-declarations}

The indexer-declarator takes one of the forms:

type this [formal-index-parameter-list]
type interface-type.this [formal-index-parameter-list]

The formal-index-parameter takes the form:

[attributes] type identifier

where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see C# Attributes.
modifiers (Optional)
Allowed modifiers are new and a valid combination of the four access modifiers.
indexer-declarator
Includes the type of the element introduced by the indexer, this, and the formal-index-parameter-list. If the indexer is an explicit interface member implementation, the interface-type is included.
formal-index-parameter-list
Specifies the parameters of the indexer. The parameter includes optional attributes, the index type, and the index identifier. At least one parameter must be specified. The parameters out and ref are not allowed.
accessor-declarations
The indexer accessors, which specify the executable statements associated with reading and writing indexer elements.

The get Accessor

The get accessor body of an indexer is similar to a method body. It returns the type of the indexer. The get accessor uses the same formal-index-parameter-list as the indexer. For example:

get {
   return myArray[index];
}

The set Accessor

The set accessor body of an indexer is similar to a method body. It uses the same formal-index-parameter-list as the indexer, in addition to the value implicit parameter. For example:

set {
   myArray[index] = value;
}

Remarks

The type of an indexer and each of the types referenced in the formal-index-parameter-list must be at least as accessible as the indexer itself. For more information on accessibility levels, see Access Modifiers.

The signature of an indexer consists of the number and types of its formal parameters. It does not include the indexer type or the names of the formal parameters.

If you declare more than one indexer in the same class, they must have different signatures.

An indexer value is not classified as a variable; therefore, it is not possible to pass an indexer value as a ref or out parameter.

In order to provide the indexer a name that other languages can use for the default indexed property, use a name attribute in the declaration. For example:

   [name ("MyItem")]
   public int this [int index] {   // indexer declaration

This indexer will have the name MyItem. Without providing the name attribute, the default name will be Item.

Example

This example declares a private array field myArray and an indexer. Using the indexer allows direct access to the instance b[i]. The alternative to using the indexer is to declare the array as a public member and access its members, myArray[i], directly.

// Indexers
using System;
class IndexerClass {
      private int [] myArray = new int[100]; 
      public int this [int index] {   // indexer declaration
      get {
         // Check the index limits
         if (index < 0 || index >= 100)
            return 0;
         else
            return myArray[index];
      }
      set {
         if (!(index < 0 || index >= 100))
            myArray[index] = value;
      }
   }
}
public class MainClass {
   public static void Main() {
      IndexerClass b = new IndexerClass();
      // call the indexer to initialize the elements #3 and #5:
      b[3] = 256;
      b[5] = 1024;
      for (int i=0; i<=10; i++) {
         Console.WriteLine("Element #{0} = {1}", i, b[i]);
      }
   }
}

Output

Element #0 = 0
Element #1 = 0
Element #2 = 0
Element #3 = 256
Element #4 = 0
Element #5 = 1024
Element #6 = 0
Element #7 = 0
Element #8 = 0
Element #9 = 0
Element #10 = 0

Notice that when an indexer's access is evaluated (for example, in a Console.Write statement) the get accessor is invoked. Therefore, if no get accessor exists, a compile-time error occurs.

See also the example on Indexers in the language reference, §10.8.

See Also

Properties | Accessors | Indexers