articles

Home / DeveloperSection / Articles / Indexer in C#

Indexer in C#

Anupam Mishra4489 21-Dec-2015

Indexer allows an object to be indexed in similar manner to be as an array. When you define an indexer. For a class the class behaves, similar to virtual array. Using property you can returns a value by using get accessor and assign a value by the set accessor. Indexers are not indexing by using any integer value, it is up to you how to define the specific lookup. The ‘this ‘keyword is used to define the indexers. We have also be overloaded the indexer in the class. You can have used more than one formal parameter, just like 2D array. The syntax of indexer is as follows:

<Access specifier><return type> this[ int index ]
{
get{  //return the value specified by index}
set {   //assign the value specified by index }
}

If you define indexer in a class then it allows client code to use [] notation on the class instance itself to assigning the values.

Advantage of Indexer:

In a class property is always identified by its name but an indexer is identified by its signature.

In a property can be a static member, but an indexer is always an instance member.

In a property a get accessor of corresponds to a method with no parameters, whereas an indexer a get accessor of corresponds to a method with the same formal parameter list as the indexer.

A set accessor of a property corresponds to a method with a single parameter named value, whereas a set accessor of an indexer corresponds to a method with the same formal parameter list as the indexer, plus an additional parameter named value.

An indexer accessor to declare a local variable with the same name as an indexer parameter. It is a compile-time error .

For example,  we define a string array of size of 10. And define two indexer for one is inserting string object and another one is checking string object index in the previous inserting.

using System;
namespace IndexerEx
{
    class Program
    {
        private string[] Name = new string[size];
        static public int size = 10;
        public Program()
        {
            for (int i = 0; i < size; i++)
            {
                 Name[i] = "Empty";
            }
        }
        public string this[int index]
        {
            get {
                string tmp;
                if (index >= 0 && index <= size - 1)
                {
                      tmp = Name[index];
                }
                else {
                    tmp = "";
                }
                     return(tmp);
            }
            set {
                if (index >= 0 && index <= size - 1)
                {
                       Name[index] = value;  
                }
            }
        }
        publicintthis[string str]
        {
             get{
                       int index = 0;
                while(index < size)
              {
                if (Name[index] == str)
               {
                    return index;
               }
               index++;
              }
                    return index;
              }
        }
        staticvoid Main(string[] args)
        {
            Console.WriteLine(@"## Let us try to use Indexer by using  string objects to check empty and non empty");
            Console.WriteLine();
            IndexerEx.Program Obj = new IndexerEx.Program();
           //assigning values to the objects
            Obj[0]="Anupam";
            Obj[1] = "Ashish";
            Obj[2]="Abhinav";
            //Printing values
            for (int i = 0; i < Program.size; i++)
            {
                Console.WriteLine(Obj[i]);
            }
            Console.WriteLine();
            Console.WriteLine("Checking 'Abhinav' index no in the existing object: ");
            Console.WriteLine();
            Console.WriteLine(Obj["Abhinav"]);
            Console.ReadKey();
         }
    }
}

 Output:
Indexer in C#


Updated 07-Sep-2019

Leave Comment

Comments

Liked By