An indexer is a specialized class, struct, or interface member that allows instances of a type to be accessed using array access syntax (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.
[]). 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 thethis 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.
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 bevirtual,abstract, oroverride. They cannot bestatic.
Implementation Examples
Basic Single-Parameter Indexer This example demonstrates an indexer wrapping a standard array backing store, utilizing expression-bodied accessors.string.
Interface Indexers
Indexers can be declared within interfaces. Interface indexers do not have a body; they only declare the required accessors.Master C# with Deep Grasping Methodology!Learn More





