Concept of Indexer
Indexer allow/enables an object to access the member of that class using an
index notation.
- Indexer exits only in C#.
- If we want to use our object as an array we need to create indexer in our
class.
or
If we want to use member variable our class to be indexed like an array for that object, this can be done by indexer. - Use for security purpose so that no one can access our private variable.
- It contain two accessors set & get.
Syntax
this
[Parameter]
{
get
{
// Get codes goes here
}
set
{
// Set codes goes here
}
}
Properties of Indexer
- Indexer increase usability.
- Indexer may be abstract.
- Indexer modifier can be private, public, protected or internal.
- Indexer are uniquely identified by their parameters.
- Indexer support inheritance.
- The return type can be any valid C# types.
- Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.
- Outsider can't access & see that member of our class. In this case we are doing operator overloading of ( [ ] )subscripts.

Join the conversation! Your thoughts help the community grow.