Indexer (programming)
Encyclopedia
In programming, an indexer is in object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 a kind of smart array that enables the user to get an index of objects held within an object. It is a member of a class that enables the use it like an array
Array
In computer science, an array data structure or simply array is a data structure consisting of a collection of elements , each identified by at least one index...

. For instance, in C#, it is possible to access with an indexer a class like an array.

Implementation

Indexers are implemented through the get and set accessor
Mutator method
In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation...

s for the . They are similar to properties
Property (programming)
A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field and a method. Properties are read and written like fields, but property reads and writes are translated to get and set method calls...

, but differ from them in not being static. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicite parameter.

Example

Here an example of the usage of an indexer in a class:

class OurFamily
{
private long[] familyMember = new long[7];
public long this [int index]
{
// The get accessor
get
{
return familyMember[index];
}

// The set accessor with
set
{
familyMember[index] = value;
}
}
}
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK