Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 17 Classesprevious at this levelnext at this level 17.8 Indexers Paragraph 11 An indexer is a member that enables an object to be indexed in the same way as an array. 2 Indexers are declared using indexer-declarations: indexer-declaration : attributesopt indexer-modifiersopt indexer-declarator { accessor-declarations } indexer-modifiers : indexer-modifier indexer-modifiers indexer-modifier indexer-modifier : new public protected internal private virtual sealed override abstract extern indexer-declarator : type this [ formal-parameter-list ] type interface-type . this [ formal-parameter-list ] Paragraph 21 An indexer-declaration may include a set of attributes (§24) and a valid combination of the four access modifiers (§17.2.3), the new (§17.2.2), virtual (§17.5.3), override (§17.5.4), sealed (§17.5.5), abstract (§17.5.6), and extern (§17.5.7) modifiers. Paragraph 31 Indexer declarations are subject to the same rules as method declarations (§17.5) with regard to valid combinations of modifiers, with the one exception being that the static modifier is not permitted on an indexer declaration. Paragraph 41 The modifiers virtual, override, and abstract are mutually exclusive except in one case. 2 The abstract and override modifiers may be used together so that an abstract indexer can override a virtual one. Paragraph 51 The type of an indexer declaration specifies the element type of the indexer introduced by the declaration. 2 Unless the indexer is an explicit interface member implementation, the type is followed by the keyword this. 3 For an explicit interface member implementation, the type is followed by an interface-type, a ".", and the keyword this. 4 Unlike other members, indexers do not have user-defined names. Paragraph 61 The formal-parameter-list specifies the parameters of the indexer. 2 The formal parameter list of an indexer corresponds to that of a method (§17.5.1), except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted. Paragraph 71 The type of an indexer and each of the types referenced in the formal-parameter-list must be at least as accessible as the indexer itself (§10.5.4). Paragraph 81 The accessor-declarations (§17.6.2), which must be enclosed in "{" and "}" tokens, declare the accessors of the indexer. 2 The accessors specify the executable statements associated with reading and writing indexer elements. Paragraph 91 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. 2 Thus, it is not possible to pass an indexer element as a ref or out argument. Paragraph 101 The formal-parameter-list of an indexer defines the signature (§10.6) of the indexer. 2 Specifically, the signature of an indexer consists of the number and types of its formal parameters. 3 The element type and names of the formal parameters are not part of an indexer's signature. Paragraph 111 The signature of an indexer must differ from the signatures of all other indexers declared in the same class. Paragraph 121 Indexers and properties are very similar in concept, but differ in the following ways: Paragraph 131 Aside from these differences, all rules defined in §17.6.2 and §17.6.3 apply to indexer accessors as well as to property accessors. Paragraph 141 When an indexer declaration includes an extern modifier, the indexer is said to be an external indexer. 2 Because an external indexer declaration provides no actual implementation, each of its accessor-declarations consists of a semicolon. [Example: The example below declares a BitArray class that implements an indexer for accessing the individual bits in the bit array.
using System;  
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[] (since each value of the former occupies only one bit instead of the latter's 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[]. end example] [Example: The following example shows a 26×10 grid class that has an indexer with two parameters. The first parameter is required to be an upper-or lowercase letter in the range A-Z, and the second is required to be an integer in the range 0-9.
using System;  
class Grid  
{  
   const int NumRows = 26;  
   const int NumCols = 10;  
   int[,] cells = new int[NumRows, NumCols];  
   
   public int this[char c, int colm]  
   {  
      get {  
         c = Char.ToUpper(c);  
         if (c < 'A' || c > 'Z') {  
            throw new ArgumentException();  
         }  
         if (colm < 0 || colm >= NumCols) {  
            throw new IndexOutOfRangeException();  
         }  
         return cells[c - 'A', colm];  
      }  
      set {  
         c = Char.ToUpper(c);  
         if (c < 'A' || c > 'Z') {  
            throw new ArgumentException();  
         }  
         if (colm < 0 || colm >= NumCols) {  
            throw new IndexOutOfRangeException();  
         }  
         cells[c - 'A', colm] = value;  
      }  
   }  
}  
end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS