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 8 Language Overviewprevious at this levelnext at this level 8.7 Classesprevious at this levelnext at this level 8.7.7 IndexersAn 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. 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. 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
using System;  
public class Stack  
{  
   private Node GetNode(int index) {  
      Node temp = first;   
      while (index > 0) {  
         temp = temp.Next;  
         index--;  
      }  
      return temp;  
   }  
   public object this[int index] {  
      get {  
         if (!ValidIndex(index))  
         throw new Exception("Index out of range.");  
         else  
         return GetNode(index).Value;  
      }  
      set {  
         if (!ValidIndex(index))  
         throw new Exception("Index out of range.");  
         else  
         GetNode(index).Value = value;  
      }  
   }  
   ...  
}  
class Test  
{  
   static void Main() {  
      Stack s = new Stack();  
      s.Push(1);  
      s.Push(2);  
      s.Push(3);  
      s[0] = 33;  // Changes the top item from 3 to 33  
      s[1] = 22;  // Changes the middle item from 2 to 22  
      s[2] = 11;  // Changes the bottom item from 1 to 11  
   }  
}  
shows an indexer for the Stack class.
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS