Thursday, April 15, 2010

Indexer


As you know, array indexing is performed using the [ ] operator. It is possible to overload the [ ] operator for classes that you create, but you don’t use an operator method. Instead, you create an indexer. An indexer allows an object to be indexed like an array. The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints.
element-type this[int index] {
  // The get accessor.
  get {
    // return the value specified by index
  }

  // The set accessor.
  set {
    // set the value specified by index
  }
}
Here, element-type is the base type of the indexer. Thus, each element accessed by the indexer will be of type element-type. The element type corresponds to the base type of an array. The parameter index receives the index of the element being accessed. Technically, this parameter does not have to be of type int, but since indexers are typically used to provide array indexing, an integer type is customary.
Inside the body of the indexer are defined two accessors that are called get and set. An accessor is similar to a method except that it does not have a return type or parameter declarations. The accessors are automatically called when the indexer is used, and both accessors receive index as a parameter. If the indexer is on the left side of an assignment statement, then the set accessor is called, and the element specified by index must be set. Otherwise, the get accessor is called, and the value associated with index must be returned. The set accessor also receives an implicit parameter called value, which contains the value being assigned to the specified index.
} 

For Example:

using System;
using System.Collections.Generic;
using System.Text;

namespace Indexers
{
    class ParentClass
    {
        private string[] range = new string[5];
        public string this[int indexrange]
        {
            get
            {
                return range[indexrange];
            }
            set
            {
                range[indexrange] = value;
            }
        }
    }

    /* The Above Class just act as array declaration using this pointer */

    class childclass
    {
        public static void Main()
        {
            ParentClass obj = new ParentClass();

            /* The Above Class ParentClass  create one object name is obj */

            obj[0] = "ONE";
            obj[1] = "TWO";
            obj[2] = "THREE";
            obj[3] = "FOUR ";
            obj[4] = "FIVE";
            Console.WriteLine("WELCOME TO YMCA \n");
            Console.WriteLine("\n");

            Console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n,{4}\n", obj[0], obj[1], obj[2], obj[3], obj[4]);
            Console.WriteLine("\n");
            Console.WriteLine("ADITYA TYAGI\n");
            Console.WriteLine("\n");
            Console.ReadLine();
        }
    }
}

Indexers Do Not Require an Underlying Array

It is important to understand that there is no requirement that an indexer actually operate on an array. It simply must provide functionality that appears “array-like” to the user of the indexer. For example, the following program has an indexer that acts like a read-only array that contains the powers of 2 from 0 to 15. Notice, however, that no actual array exists. Instead, the indexer simply computes the proper value for a given index.
// Indexers don't have to operate on actual arrays.
 
using System;
 
class PwrOfTwo {
 
  /* Access a logical array that contains
     the powers of 2 from 0 to 15. */
  public int this[int index] {
    // Compute and return power of 2.
    get {
      if((index >= 0) && (index < 16)) return pwr(index);
      else return -1;
    }
 
    // there is no set accessor
  }
 
  int pwr(int p) {
    int result = 1;
    for(int i=0; i < p; i++)
      result *= 2;
 
    return result;
  }
}
 
class UsePwrOfTwo {
  public static void Main() {
    PwrOfTwo pwr = new PwrOfTwo();
 
    Console.Write("First 8 powers of 2: ");
    for(int i=0; i < 8; i++)
      Console.Write(pwr[i] + " ");
    Console.WriteLine();
 
    Console.Write("Here are some errors: ");
    Console.Write(pwr[-1] + " " + pwr[17]);
 
    Console.WriteLine();
  }
}
The output from the program is shown here:
First 8 powers of 2: 1 2 4 8 16 32 64 128
Here are some errors: -1 -1
Notice that the indexer for PwrOfTwo includes a get accessor, but no set accessor. As explained, this means that the indexer is read-only. Thus, a PwrOfTwo object can be used on the right side of an assignment statement, but not on the left. For example, attempting to add this statement to the preceding program won’t work:
pwr[0] = 11; // won't compile
This statement will cause a compilation error because there is no set accessor defined for the indexer.
There are two important restrictions to using indexers. First, because an indexer does not define a storage location, a value produced by an indexer cannot be passed as a ref or out parameter to a method. Second, an indexer must be an instance member of its class; it cannot be declared static.

No comments:

Post a Comment