Latest web development tutorials

C # indexer (Indexer)

Indexer (Indexer) allows an object like an array can be indexed.When you define an indexer for the class, class acts will be like avirtual array (virtual array) the same.You can use the array access operator ([]) to access the instance of the class.

grammar

A dimensional indexing the following syntax:

element-type this [int index] 
{
   // Get accessor get 
   {
      // Returns the value of the specified index}

   // Set accessor set 
   {
      // Set the value of the specified index}
}

Indexer (Indexer) uses

Statement acts in a way similar to the indexer property (property). Like property (property), that you can use to define the indexset and getaccessor. However, the property is returned or set a specific data member, and the index returns or sets the object instance to a specific value. In other words, it is the instance data is divided into smaller parts, and each part of the index, get or set each section.

The definition of an attribute (property) comprises providing property names. When the index is not defined with the name, but withthis key, which points to the object instance.The following example illustrates this concept:

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string [] namelist = new string [size];
      static public int size = 10;
      public IndexedNames ()
      {
         for (int i = 0; i <size; i ++)
         namelist [i] = "NA";
      }
      public string this [int index]
      {
         get
         {
            string tmp;

            if (index> = 0 && index <= size-1)
            {
               tmp = namelist [index];
            }
            else
            {
               tmp = "";
            }

            return (tmp);
         }
         set
         {
            if (index> = 0 && index <= size-1)
            {
               namelist [index] = value;
            }
         }
      }

      static void Main (string [] args)
      {
         IndexedNames names = new IndexedNames ();
         names [0] = "Zara";
         names [1] = "Riz";
         names [2] = "Nuha";
         names [3] = "Asif";
         names [4] = "Davinder";
         names [5] = "Sunil";
         names [6] = "Rubic";
         for (int i = 0; i <IndexedNames.size; i ++)
         {
            Console.WriteLine (names [i]);
         }
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
NANANA

Overloaded indexer (Indexer)

Indexer (Indexer) may be overloaded. When indexer declaration can also be provided with a plurality of parameters, and each parameter can be of different types. There is no need for the index must be an integer. C # allows indexers may be other types, such as string type.

The following example demonstrates overloaded indexer:

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string [] namelist = new string [size];
      static public int size = 10;
      public IndexedNames ()
      {
         for (int i = 0; i <size; i ++)
         {
          namelist [i] = "NA";
         }
      }
      public string this [int index]
      {
         get
         {
            string tmp;

            if (index> = 0 && index <= size-1)
            {
               tmp = namelist [index];
            }
            else
            {
               tmp = "";
            }

            return (tmp);
         }
         set
         {
            if (index> = 0 && index <= size-1)
            {
               namelist [index] = value;
            }
         }
      }
      public int this [string name]
      {
         get
         {
            int index = 0;
            while (index <size)
            {
               if (namelist [index] == name)
               {
                return index;
               }
               index ++;
            }
            return index;
         }

      }

      static void Main (string [] args)
      {
         IndexedNames names = new IndexedNames ();
         names [0] = "Zara";
         names [1] = "Riz";
         names [2] = "Nuha";
         names [3] = "Asif";
         names [4] = "Davinder";
         names [5] = "Sunil";
         names [6] = "Rubic";
         // Use the first parameter with int indexer for (int i = 0; i <IndexedNames.size; i ++)
         {
            Console.WriteLine (names [i]);
         }
         // Use the second indexer with a string parameter Console.WriteLine (names [ "Nuha"]);
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
NANANA
2