In C#, an indexer allows objects of a class or struct to be accessed using array-like syntax. When an indexer is defined, the object can be indexed using the array access operator ([ ]), just like arrays or collections.
Indexers make classes behave like virtual arrays, enabling users to retrieve or assign values directly through indices rather than using explicit method calls.
Syntax
[access_modifier] [return_type] this[parameter_list]
{
get { // code to return value }
set { // code to assign value }
}
- access_modifier: Specifies accessibility (e.g. public, private, protected, internal)
- return_type: Defines the type of value the indexer returns
- this: Refers to the current instance of the class
- parameter_list: Specifies the index parameters used to access class elements
- get / set: Accessors used to read and assign values respectively
Main difference between Indexers and Properties:
- A property accessor (get/set) does not take parameters.
- An indexer accessor (get/set) must take at least one parameter.
Example: Simple Indexer
OutputFirst value: C
Second value: C++
Third value: C#
The class Geeks defines an indexer that enables direct access to elements through array-like syntax (obj[0], obj[1], etc.).
Types of Indexers
In C#, indexers can be classified based on the number of parameters they accept. They allow objects to be accessed using array-like syntax, making them intuitive and flexible for data manipulation.
1. Single-Dimensional Indexer
- A single-dimensional indexer takes one parameter, typically an integer similar to how elements are accessed in a one-dimensional array.
- It is the most common form of indexer used to represent sequential data like lists or collections.
Syntax:
Usage:
- Uses one index parameter.
- Provides simple array-like access to class elements.
- Commonly used for custom collection classes or wrapper types.
2. Multi-Dimensional Indexer
- A multi-dimensional indexer takes multiple parameters, similar to multi-dimensional arrays (e.g., 2D or 3D arrays).
- This type of indexer is useful for classes that represent tabular or grid-like data structures.
Syntax:
Usage:
- Takes two or more parameters.
- Enables indexed access for complex data structures such as matrices or coordinate maps.
- Each parameter represents a dimension, e.g., row and column in a 2D structure.
Implementing Indexers
1. Multi-Parameter Indexers
Indexers can take multiple parameters, enabling access to data through complex indexing.
2. Overloading Indexers
Just like methods, indexers can be overloaded using different parameter types or counts.
3. Read-Only Indexers
If only a get accessor is defined, the indexer becomes read-only.
4. Indexers in Interfaces
Indexers can be declared in interfaces and implemented by classes.
If multiple interfaces have indexers with the same signature, you can use explicit implementation to avoid conflicts.
5. Indexers in Collection Classes
Custom collection classes often use indexers to provide direct element access.