C# Basics: Indexer

An indexer allows us to index an array. We need to define an indexer for a class. An indexer is similar to a property. As with properties, you use get and set when defining an indexer. Unlike propertyies, indexers are not defined with names, but with the “this” keyword, that refers to the object instance. Once we have defined an indexer for a class then we can access its object with the array access operator ([ ]).

A one-dimensional indexer has the following syntax:

    element-type this[int index]
    {

    get
    {
    //getting the value
    }

    set
    {
    // setting the value
    }
    }

Example

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace PracticeIndexer  
  8. {  
  9.     public class clsPerson  
  10.     {  
  11.         public string Name { getset; }  
  12.         public clsPerson(string name)  
  13.         {  
  14.             Name = name;  
  15.         }  
  16.     }  
  17.   
  18.     class clsPersonIndexer  
  19.     {  
  20.         private clsPerson[] myData;  
  21.   
  22.         public clsPersonIndexer(int size)  
  23.         {  
  24.             myData = new clsPerson[size];  
  25.   
  26.             for (int i = 0; i < size; i++)  
  27.             {  
  28.                 myData[i] = new clsPerson(string.Empty);  
  29.             }  
  30.         }  
  31.         public string this[int pos]  
  32.         {  
  33.             get  
  34.             {  
  35.                 return "Hi " + myData[pos].Name + "..";  
  36.             }  
  37.             set  
  38.             {  
  39.                 myData[pos].Name = value;  
  40.             }  
  41.         }  
  42.   
  43.     }  
  44.     class Program  
  45.     {  
  46.         static void Main(string[] args)  
  47.         {  
  48.             int size = 10;  
  49.             clsPersonIndexer myValue = new clsPersonIndexer(size);  
  50.             myValue[1] = "ratnesh";  
  51.             myValue[7] = "swati";  
  52.             myValue[5] = "karunesh";  
  53.             Console.WriteLine("\nIndexer Output\n");  
  54.             for (int i = 0; i < size; i++)  
  55.             {  
  56.                 Console.WriteLine(myValue[i]);  
  57.             }  
  58.             Console.WriteLine("\nPress enter to exit..\n");  
  59.             Console.ReadLine();  
  60.   
  61.         }  
  62.     }  
  63. }  
Output

run program

In this example we have created an indexer in the class clsPersonIndexer. This class contains an array of clsPerson called “myData” to store a list of objects of the class clsPerson. In the constructor we have initialized the clsPerson objects with an empty string. In the indexer we print the great message in “get”. Now in the client code (main function) we are trying to access an element with random order to set the value.

Indexers can be declared with multiple parameters and each parameter may be a different type. These types of indexers are known as an overloaded Indexer. Indexer parameters can be of types integers, enums and strings.

Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace PracticeIndexer  
  8. {  
  9.     public class clsPerson  
  10.     {  
  11.         public string Name { getset; }  
  12.         public int Score { getset; }  
  13.         public clsPerson(string name, int score)  
  14.         {  
  15.             Name = name;  
  16.             Score = score;  
  17.         }  
  18.     }  
  19.   
  20.     class clsPersonIndexer  
  21.     {  
  22.         private clsPerson[] myData;  
  23.   
  24.         public clsPersonIndexer(int size)  
  25.         {  
  26.             myData = new clsPerson[size];  
  27.   
  28.             for (int i = 0; i < size; i++)  
  29.             {  
  30.                 myData[i] = new clsPerson(string.Empty,0);  
  31.             }  
  32.         }  
  33.         public string this[int pos]  
  34.         {  
  35.             get  
  36.             {  
  37.                 return "Hi " + myData[pos].Name + "..";  
  38.             }  
  39.             set  
  40.             {  
  41.                 Random rnd = new Random();  
  42.                 myData[pos].Name = value;  
  43.                 myData[pos].Score= Convert.ToInt16( DateTime.Now.Ticks%100);  
  44.             }  
  45.         }  
  46.   
  47.         public string this[int pos, int marks]  
  48.         {  
  49.             get  
  50.             {  
  51.                 string info = "";  
  52.                 if (myData[pos].Name != string.Empty)  
  53.                 {  
  54.                     if (myData[pos].Score > marks)  
  55.                     {  
  56.                         info = "Hi " + myData[pos].Name + "(Score: " + myData[pos].Score + "), you are qualified..";  
  57.                     }  
  58.                     else  
  59.                     {  
  60.                         info = "Hi " + myData[pos].Name + "(Score: " + myData[pos].Score + "), you are not qualified..";  
  61.                     }  
  62.                 }  
  63.                 return info;  
  64.             }  
  65.         }  
  66.     }  
  67.     class Program  
  68.     {  
  69.         static void Main(string[] args)  
  70.         {  
  71.             int size = 10;  
  72.             clsPersonIndexer myValue = new clsPersonIndexer(size);  
  73.             myValue[1] = "ratnesh";  
  74.             myValue[7] = "swati";  
  75.             myValue[5] = "karunesh";  
  76.             Console.WriteLine("\n ...Indexer Output...\n");  
  77.             for (int i = 0; i < size; i++)  
  78.             {  
  79.                 Console.WriteLine(myValue[i]);  
  80.             }  
  81.             Console.WriteLine("\n ...Overloaded Indexer Output...\n");  
  82.            
  83.             for (int i = 0; i < size; i++)  
  84.             {  
  85.                 string info=myValue[i,60];  
  86.                 if (info != string.Empty)  
  87.                     Console.WriteLine(info);  
  88.             }    
  89.               
  90.             Console.WriteLine("\n ...Press enter to exit...\n");  
  91.             Console.ReadLine();  
  92.   
  93.         }  
  94.     }  
  95. }  
output

 


Similar Articles