Indexer (programming)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

Lua error in package.lua at line 80: module 'strict' not found. In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays.[1]

Implementation

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.

Example

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
    get
    {
      return familyMember[index];
    }

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

See also

References

<templatestyles src="Reflist/styles.css" />

Cite error: Invalid <references> tag; parameter "group" is allowed only.

Use <references />, or <references group="..." />


<templatestyles src="Asbox/styles.css"></templatestyles>

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.