Jump to content

Indexer (programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mild Bill Hiccup (talk | contribs) at 01:57, 25 June 2012 (Implementation: spelling). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In programming, an indexer is in object-oriented programming 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. For instance, in C#, it is possible to access with an indexer a class like an array.[1]

Implementation

Indexers are implemented through the get and set accessors for the operator[]. They are similar to properties, 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 implicit value parameter.

Example

Here an example of the usage of an indexer in a class: [2]

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

    // The set accessor with 
    set
    {
      familyMember[index] = value;
    }
  }
}

References

  1. ^ jagadish980 (2008-01-29). "C# - What is an indexer in C#". http://forums.sureshkumar.net/forum.php: Bulletin: SURESHKUMAR.NET FORUMS. Retrieved 2011-08-01. {{cite web}}: External link in |location= (help)CS1 maint: numeric names: authors list (link)
  2. ^ "C# Interview Questions". http://www.dotnetfunda.com/: .net Funda. Retrieved 2011-08-01. {{cite web}}: External link in |location= (help)

See also