2 <clause number="8.7.7" title="Indexers" informative="true">
3 <paragraph>An indexer is a member that enables an object to be indexed in the same way as an array. Whereas properties enable field-like access, indexers enable array-like access. </paragraph>
4 <paragraph>As an example, consider the Stack class presented earlier. The designer of this class might want to expose array-like access so that it is possible to inspect or alter the items on the stack without performing unnecessary Push and Pop operations. That is, class Stack is implemented as a linked list, but it also provides the convenience of array access. </paragraph>
5 <paragraph>Indexer declarations are similar to property declarations, with the main differences being that indexers are nameless (the "name" used in the declaration is this, since this is being indexed) and that indexers include indexing parameters. The indexing parameters are provided between square brackets. The example <code_example><![CDATA[
9 private Node GetNode(int index) {
17 public object this[int index] {
19 if (!ValidIndex(index))
20 throw new Exception("Index out of range.");
22 return GetNode(index).Value;
25 if (!ValidIndex(index))
26 throw new Exception("Index out of range.");
28 GetNode(index).Value = value;
36 Stack s = new Stack();
40 s[0] = 33; // Changes the top item from 3 to 33
41 s[1] = 22; // Changes the middle item from 2 to 22
42 s[2] = 11; // Changes the bottom item from 1 to 11
45 ]]></code_example>shows an indexer for the Stack class. </paragraph>