Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An indexer is a specialized class, struct, or interface member that allows instances of a type to be accessed using array access syntax ([]). Under the hood, indexers are syntactic sugar for parameterized properties. During compilation, the C# compiler translates indexers into get_Item and set_Item methods in the Intermediate Language (IL).

Syntax and Declaration

Indexers are declared using the this keyword in place of a member name, followed by the index parameter(s) enclosed in square brackets. Like standard properties, they utilize get, set, or init accessors.
public ReturnType this[IndexType index]
{
    get { /* return the value specified by index */ }
    set { /* assign the value specified by index */ }
}

Technical Characteristics

  • Signature Matching: An indexer’s signature is defined by the number and types of its parameters. The return type is not part of the signature.
  • Overloading: A single type can define multiple indexers, provided each has a distinct parameter signature.
  • Non-Integer Indices: Unlike standard arrays, which strictly require integer indices, indexers can accept parameters of any type (e.g., string, Guid, enum).
  • Multidimensionality: Indexers can accept multiple parameters to simulate multidimensional array access.
  • Expression-Bodied Members: Indexers fully support expression-bodied syntax (=>) for concise accessor implementation.
  • Modifiers: Indexers can be marked with access modifiers (public, protected, etc.), and can be virtual, abstract, or override. They cannot be static.

Implementation Examples

Basic Single-Parameter Indexer This example demonstrates an indexer wrapping a standard array backing store, utilizing expression-bodied accessors.
public class DataStore
{
    private readonly string[] _backingArray = new string[10];

    public string this[int index]
    {
        get => _backingArray[index];
        set => _backingArray[index] = value;
    }
}
Overloaded Indexer with Non-Integer Type This demonstrates indexer overloading where the index parameter is a string.
public class ConfigurationMap
{
    private readonly Dictionary<string, string> _settings = new();

    public string this[string key]
    {
        get => _settings.TryGetValue(key, out var val) ? val : null;
        set => _settings[key] = value;
    }
}
Multidimensional Indexer Indexers can declare multiple parameters separated by commas.
public class Grid
{
    private readonly int[,] _matrix = new int[10, 10];

    public int this[int row, int column]
    {
        get => _matrix[row, column];
        set => _matrix[row, column] = value;
    }
}

Interface Indexers

Indexers can be declared within interfaces. Interface indexers do not have a body; they only declare the required accessors.
public interface IRegistry
{
    // Declares an indexer requiring both a getter and a setter
    object this[Guid id] { get; set; }
}
When implementing an interface indexer, the implementing class must match the exact signature and provide the accessors mandated by the interface. Explicit interface implementation is also supported to hide the indexer from the class’s default public API.
Master C# with Deep Grasping Methodology!Learn More