Saturday, March 28, 2009

Indexers

class indexerDemo
   {
       int[] arr = new int[] { 34, 344, 8, 34, 62, 625, 342, 546 };
       public int Lenght()
       {
           return arr.Length;
       }
       public int this[int index]
       {
           get
           {
               return arr[index];
           }
           set
           {
               arr[index] = value;
           }
       }
   }
 
   class Program
   {
       static void Main(string[] args)
       {
           indexerDemo indexD = new indexerDemo();
           Console.WriteLine(indexD[0].ToString());
       }
   }

Above code can be used to add indexing to any custom class.