In C++ one can emulate indexing by overloading the [] operator. The expression a[b...] translates to a call to the user-defined function operator[] as (a).operator[](b...).2 Here is an example,
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: 3
Usage example:
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:
In PHP indexing can be implemented via the predefined ArrayAccess interface,4
In Python one implements indexing by overloading the __getitem__ and __setitem__ methods,
Rust provides the std::ops::Index trait.5
In Smalltalk one can emulate indexing by (e.g.) defining the get: and set:value: instance methods. For example, in GNU Smalltalk,
jagadish980 (2008-01-29). "C# - What is an indexer in C#". SURESHKUMAR.NET FORUMS. Archived from the original on September 22, 2009. Retrieved 2011-08-01.{{cite web}}: CS1 maint: numeric names: authors list (link) https://web.archive.org/web/20090922193214/http://forums.sureshkumar.net/vb-asp-net-interview-technical-questions/16320-c-what-indexer-c.html ↩
"C++ operator overloading". https://en.cppreference.com/w/cpp/language/operators ↩
"C# Interview Questions". .net Funda. Retrieved 2011-08-01. https://www.dotnetfunda.com/interview/exam4161-what-is-an-indexer-in-csharp-.aspx ↩
"PHP ArrayAccess interface". https://www.php.net/manual/en/class.arrayaccess.php ↩
"Index in std::ops - Rust". doc.rust-lang.org. Retrieved 11 January 2025. https://doc.rust-lang.org/std/ops/trait.Index.html ↩