What are indexers in C#
An indexer allows an object to be indexed like an array. It is useful to support the creation of specialized arrays respective to one or more constraints. Indexers allows a class to be accessed the same way as an array is access means object of a class be indexed same way as an array. Modifier of an indexer can be of public, private, protected and internal. The return type can be any valid C# types . The indexing is performed using the [ ] operator.
An indexer is also similar to a property. As with properties, you use get and set when defining an indexer . Unlike properties, you are not obtaining a specific data member; rather, you are obtaining a value from the object itself.
Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers. Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error .
When should you use C# indexers?
You can typically use an indexer if the class represents a list, collection or array of objects.