In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of " objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, an indexer allows instances of a particular class or struct to be indexed just like arrays. It is a form of
operator overloading
In computer programming, operator overloading, sometimes termed ''operator ad hoc polymorphism'', is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading i ...
.
Implementation
Indexers are implemented through the get and set
accessors for the . They are similar to
properties
Property is the ownership of land, resources, improvements or other tangible objects, or intellectual property.
Property may also refer to:
Mathematics
* Property (mathematics)
Philosophy and science
* Property (philosophy), in philosophy an ...
, but differ by not being
static, and the fact that indexers' accessors take parameters. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicit parameter.
Example
Here is a C# example of the usage of an indexer in a class:
class Family
Usage example:
void Main()
In this example, the indexer is used to get the value at the nth position, and then to get the position in the list referenced by its value.
The output of the code is:
John is the member number 0 of the doeFamily
Jane is the member number 1 of the doeFamily
See also
*
Mutator method
References
Programming language topics
Object-oriented programming
Operators (programming)
{{compu-prog-stub