Indexers permit instances of a class to be indexed in the same way as arrays. Indexers are declared using indexer-declarations:
An indexer-declaration may include set of attributes (§17), a new
modifier (§10.2.2), and a valid combination of the four access modifiers (§10.2.3).
The type of an indexer declaration specifies the element type of the indexer introduced by the declaration. Unless the indexer is an explicit interface member implementation, the type is followed by the keyword this
. For an explicit interface member implementation, the type is followed by an interface-type, a ".
", and the keyword this
. Unlike other members, indexers do not have user-defined names.
The formal-index-parameter-list specifies the parameters of the indexer. The formal parameter list of an indexer corresponds to that of a method (§10.5.1), except that at least one parameter must be specified, and that the ref
and out
parameter modifiers are not permitted.
The type of an indexer and each of the types referenced in the formal-index-parameter-list must be at least as accessible as the indexer itself (§3.3.4).
The accessor-declarations, which must be enclosed in "{
" and "}
" tokens, declare the accessors of the indexer. The accessors specify the executable statements associated with reading and writing indexer elements.
Even though the syntax for accessing an indexer element is the same as that for an array element, an indexer element is not classified as a variable. Thus, it is not possible to pass an indexer element as a ref
or out
parameter.
The formal parameter list of an indexer defines the signature (§3.4) of the indexer. Specifically, the signature of an indexer consists of the number and types of its formal parameters. The element type is not part of an indexer’s signature, nor are the names of the formal parameters.
The signature of an indexer must differ from the signatures of all other indexers declared in the same class.
Indexers and properties are very similar in concept, but differ in the following ways:
static
member, whereas an indexer is always an instance member.get
accessor of a property corresponds to a method with no parameters, whereas a get
accessor of an indexer corresponds to a method with the same formal parameter list as the indexer.set
accessor of a property corresponds to a method with a single parameter named value
, whereas a set
accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value
.With these differences in mind, all rules defined in §10.6.2 and §10.6.3 apply to indexer accessors as well as property accessors.
The example below declares a BitArray
class that implements an indexer for accessing the individual bits in the bit array.
class BitArray { int[] bits; int length; public BitArray(int length) { if (length < 0) throw new ArgumentException(); bits = new int[((length - 1) >> 5) + 1]; this.length = length; } public int Length { get { return length; } } public bool this[int index] { get { if (index < 0 || index >= length) { throw new IndexOutOfRangeException(); } return (bits[index >> 5] & 1 << index) != 0; } set { if (index < 0 || index >= length) { throw new IndexOutOfRangeException(); } if (value) { bits[index >> 5] |= 1 << index; } else { bits[index >> 5] &= ~(1 << index); } } } }
An instance of the BitArray
class consumes substantially less memory than a corresponding bool[]
(each value occupies only one bit instead of one byte), but it permits the same operations as a bool[]
.
The following CountPrimes
class uses a BitArray
and the classical "sieve" algorithm to compute the number of primes between 1 and a given maximum:
class CountPrimes { static int Count(int max) { BitArray flags = new BitArray(max + 1); int count = 1; for (int i = 2; i <= max; i++) { if (!flags[i]) { for (int j = i * 2; j <= max; j += i) flags[j] = true; count++; } } return count; } static void Main(string[] args) { int max = int.Parse(args[0]); int count = Count(max); Console.WriteLine("Found {0} primes between 1 and {1}", count, max); } }
Note that the syntax for accessing elements of the BitArray
is precisely the same as for a bool[]
.