encyclopegia.org

Indexer (programming)

In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays. It is a form of operator overloading.

In object-oriented programming, an indexer allows instances of a particular class or struct to be indexed just like arrays. It is a form of operator overloading. import std; class Vec { private: size_t size; double data[]; public: explicit Vec(size_t n): size{n}, data{new double[n]} {} ~Vec() { delete[] data; } double& operator[](size_t i) noexcept { return data[i]; } }; int main() { Vec v(3); for (size_t i = 0; i ==== Example 1==== namespace Wikipedia.Examples; using System; public class Vec { private double[] _data; public Vec(int n) { _data = new double[n]; } public int Size => _data.Length; public double this[int i] { get => _data[i]; set => _data[i] = value; } static void Main() { Vec v = new(3); for (int i = 0; i ==== Example 2==== Here is a C# example of the usage of an indexer in a class: namespace Wikipedia.Examples; using System; using System.Collections.Generic; public class Family { private List _familyMembers = new(); public Family(params string[] members) { _familyMembers.AddRange(members); } public string this[int index] { // The get accessor get => _familyMembers[index]; // The set accessor with set => _familyMembers[index] = value; } public int this[string val] { // Getting index by value (first element found) get => _familyMembers.FindIndex(m => m == val); } public int Length => _familyMembers.Count; } Usage example: using System; void Main() { Family doeFamily = new("John", "Jane"); for (int i = 0; i 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 class Vec implements ArrayAccess { function __construct(int $n) { $this->size = $n; $this->data = array_fill(0, $n, 0); } public function offsetGet($offset): mixed { return $this->data[$offset]; } public function offsetSet($offset, $value): void { $this->data[$offset] = $value; } public function offsetExists($offset): bool {} public function offsetUnset($offset): void {} } $v = new Vec(3); for ($i = 0; $i size; $i++) { $v[$i] = $i + 1; } for ($i = 0; $i size; $i++) { print "{$v[$i]}\n"; } import array class Vec(object): def __init__(self, n: int) -> None: self.size = n self.data = array.array("d", [0.0] * n) def __getitem__(self, i: int) -> int: return self.data[i] def __setitem__(self, i: int, value: int) -> None: self.data[i] = value v: Vec = Vec(3) for i in range(v.size): v[i] = i + 1 for i in range(v.size): print(v[i]) use std::ops::Index; enum Nucleotide { A, C, G, T, } struct NucleotideCount { a: u32, c: u32, g: u32, t: u32, } impl Index for NucleotideCount { type Output = usize; fn index(&self, nucleotide: Nucleotide) -> &Self::Output { match nucleotide { Nucleotide::A => &self.a, Nucleotide::C => &self.c, Nucleotide::G => &self.g, Nucleotide::T => &self.t, } } } fn main() { let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12}; assert_eq!(nucleotide_count[Nucleotide::A], 14); assert_eq!(nucleotide_count[Nucleotide::C], 9); assert_eq!(nucleotide_count[Nucleotide::G], 10); assert_eq!(nucleotide_count[Nucleotide::T], 12); } Object subclass: vector [ |data| ] vector class extend [ new: n [ |v| v:=super new. v init: n. ^v] ] vector extend [ init: n [ data:= Array new: n ] ] vector extend [ size [ ^(data size) ] ] vector extend [ get: i [ ^(data at: i) ] ] vector extend [ set: i value: x [ data at: i put: x ] ] v:=vector new: 3 1 to: (v size) do: [:i| v set: i value: (i+1) ] 1 to: (v size) do: [:i| (v get: i) printNl ]

References

No reference rows published for this article.

Cite this article

MLA

"Indexer (programming)." encyclopegia.org, 2026, https://encyclopegia.org/wiki/indexer-programming. Accessed 24 November 2026.

Chicago

"Indexer (programming)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/indexer-programming.

APA

encyclopegia.org. (2026). Indexer (programming). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/indexer-programming

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Category:Programming languages

stub · references available

programming paradigm

stub · references available

extreme programming

stub · references available

high-level programming language

stub · references available

logic programming

stub · references available

return-oriented programming

stub · references available

in-system programming

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1378382690 · 0.5 · snapshot enwiki-001378382690

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.