Accessing objects using indexers
Indexes enable objects in classes to be accessed in the same way you access items in an array. An indexer will have a modifier, a return type, the this
keyword to indicate the object of the current class, and an argument list. You will always use the this
keyword when creating an indexer. Indexer is the term given to a parameterized property. The index is created using the get
and set
accessors. You are not allowed to use the ref
or out
keywords to modify indexer parameters. A minimum of one parameter should be specified. An indexer cannot be static since it is an instance member. However, the indexer properties can be static. You would implement an indexer if you need to operate on a group of elements. The main difference between a property and an indexer is that you identify and access a property by its name. On the other hand, with an indexer, it is identified by its signature and accessed using indexes. Moreover, you can overload indexers.
Now...