Export (0) Print
Expand All

Interface Indexers

Indexers can be declared on interfaces. The declaration takes the following form:

[attributes] [new] type this [formal-index-parameter-list] {interface-accessors}

where:

attributes (Optional)
Same as indexers on classes.
type
Same as indexers on classes.
formal-index-parameter-list
Same as indexers on classes.
interface-accessors
The indexer accessors.

Remarks

Accessors of interface indexers differ from the accessors of class indexers in the following ways:

  • Interface accessors do not use modifiers.
  • An interface accessor does not have a body.

Thus, the purpose of the accessor is to indicate whether the indexer is read-write, read-only, or write-only.

The following is an example of an interface indexer accessor:

public interface IMyInterface 
{
   ...
   // Indexer declaration:
   string this[int index] 
   {
      get; 
      set; 
   }
}

The signature of an indexer must differ from the signatures of all other indexers declared in the same interface.

Example

The following example shows how to implement interface indexers.

// cs_interface_indexers.cs
using System;
// Indexer on an interface:
public interface IMyInterface 
{
   // Indexer declaration:
   int this[int index] 
   {
      get; 
      set; 
   }
}

// Implementing the interface.
class IndexerClass : IMyInterface
{
   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[2] = 4;
      b[5] = 32;
      for (int i=0; i<=10; i++) 
      {
         Console.WriteLine("Element #{0} = {1}", i, b[i]);
      }
   }
}

Output

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

In the preceding example, you could use the explicit interface member implementation by using the fully qualified name of the interface member. For example:

public string IMyInterface.this 
{
}

However, the fully qualified name is only needed to avoid ambiguity when the class is implementing more than one interface with the same indexer signature. For example, if the Employee class is implementing two interfaces, ICitizen and IEmployee, and both interfaces have the same indexer signature, the explicit interface member implementation is necessary. That is, the following indexer declaration:

public string IEmployee.this 
{
}

implements the indexer on the IEmployee interface, while the following declaration:

public string ICitizen.this 
{
}

implements the indexer on the ICitizen interface.

For more information, see 13.4.1 Explicit interface member implementations.

See Also

Indexers | interface

Show:
© 2014 Microsoft