Indexer (programming)

In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays.[1]


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


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

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

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

See also


  1. jagadish980 (2008-01-29). "C# - What is an indexer in C#". Bulletin: SURESHKUMAR.NET FORUMS. Archived from the original on September 22, 2009. Retrieved 2011-08-01.
  2. "C# Interview Questions". .net Funda. Retrieved 2011-08-01.

This article is issued from Wikipedia - version of the 9/13/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.