Indexers in C#

Introduction

 
The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints. We can use indexers for any purpose for which an array-like syntax is beneficial. Indexers can have one or more dimensions.
 
Syntax of Indexers
 
element-type this [int index]
{
     get{ return the value specified by index }; 
     set{ set the value specified by index }
}
 
"element-type" is the type of member. Each element is accessed by the indexer will be of type element-type. This type corresponds to the element type of an array.
 
"[int index]": The parameter index receives the index of the element being accessed. Technically this parameter does not need to be of type "int", but since indexes are typically used to provide array indexing, using an integer type is quite common.
 
Inside the body of the indexer, two assessors are defined that are called "get" and "set". The accessor is similar to a method except that it does not declare a return type or parameters. The accessor is automatically called when the indexer is used and both accessors receive an 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 the index must be "set", otherwise the get accessor is called and the value associated with the index must be returned. The "set" method also receives an implicit parameter called "value", which contains the value being assigned to the specified index.
 
One of the benefits of an indexer is that we can control how an array is accessed, heading off improper access.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.    
  7. namespace Indexer  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             indexerExample obj = new indexerExample(5);  
  14.             int x;  
  15.             obj[0] = 45;  
  16.             obj[1] = 3;   
  17.             obj[2] = 12;   
  18.             obj[3] = 44;   
  19.             obj[4] = 4;  
  20.             for (x = 0; x < 5; x++)  
  21.             {  
  22.                 Console.WriteLine(obj[x]);  
  23.             }  
  24.             Console.ReadKey();  
  25.    
  26.         }  
  27.     }  
  28.     public class indexerExample  
  29.     {  
  30.         int[] arr;  
  31.         public indexerExample(int arrSize)  
  32.         {  
  33.             arr = new int[arrSize];  
  34.         }  
  35.         public int this[int index_no]  
  36.         {  
  37.             get { return index_no; }  
  38.             set { index_no = value; }  
  39.         }  
  40.     }  
  41. }  
indexer
 
Indexes do not require an underlying array
 
There is no requirement that an index actually operates on an array. It simply must provide functionality that appears array-like to the user for the indexes. Let's understand it with an 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 workSir  
  8. {  
  9.     class PwrOfTwo  
  10.     {  
  11.         public int this[int index]  
  12.         {  
  13.             get  
  14.             {  
  15.                 if ((index >= 0) && (index < 16))  
  16.                     return pwr(index);  
  17.                 else  
  18.                     return -1;  
  19.             }  
  20.             //there is no set accesser  
  21.         }  
  22.         int pwr(int p)  
  23.         {  
  24.             int result = 1;  
  25.             for (int i = 0; i < p; i++)  
  26.             {  
  27.                 result = result * 2;  
  28.             }  
  29.             return result;  
  30.         }  
  31.    
  32.    
  33.         class Program  
  34.         {  
  35.             static void Main(string[] args)  
  36.             {  
  37.                 PwrOfTwo pwr = new PwrOfTwo();  
  38.                 Console.WriteLine("power of 2 :");  
  39.                 for (int i = 0; i < 8; i++)  
  40.                     Console.Write(pwr[i] + " ");  
  41.                 Console.WriteLine();  
  42.                 Console.Write("Here are some errors :");  
  43.                 Console.Write(pwr[-1] + "" + pwr[17]);  
  44.                 Console.WriteLine();  
  45.                 Console.ReadKey();  
  46.             }  
  47.         }  
  48.     }  
  49. }   
pow
 
Multi-dimensional indexer
 
We can create a multi-dimensional array. Let us understand it with the help of an 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 Indexer  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             indexerExample obj = new indexerExample(5,3);  
  14.             int x;  
  15.              
  16.             obj[0,0]=25;  
  17.             obj[0,1]=22;  
  18.             obj[0,2]=3;  
  19.               
  20.             obj[1,0]=14;  
  21.             obj[1,1]=28;  
  22.             obj[1,2]=33;  
  23.               
  24.             obj[2,0]=3;  
  25.             obj[2,1]=92;  
  26.             obj[2,2]=64;  
  27.               
  28.             obj[3,0]=30;  
  29.             obj[3,1]=66;  
  30.             obj[3,2]=55;  
  31.               
  32.             obj[4,0]=88;  
  33.             obj[4,1]=20;  
  34.             obj[4,2]=99;  
  35.               
  36.    
  37.             for (int i = 0; i < 5;i++ )  
  38.             {   
  39.                 for (x = 0; x < 3; x++)  
  40.                 {  
  41.                     Console.Write(obj[i,x] +"\t");  
  42.                    // obj[i,x]=  
  43.                 }  
  44.                 Console.WriteLine();  
  45.             }  
  46.             Console.ReadKey();  
  47.    
  48.         }  
  49.     }  
  50.     public class indexerExample  
  51.     {  
  52.         int[,] arr;  
  53.         int row, column;  
  54.         public indexerExample(int r, int c)  
  55.         {  
  56.             row = r;  
  57.             column = c;  
  58.    
  59.            arr = new int[row,column];  
  60.         }  
  61.         public int this[int index1, int index2]  
  62.         {  
  63.             get { return arr[index1,index2]; }  
  64.             set { arr[index1, index2] = value; }  
  65.         }  
  66.     }  
  67. }  
multiDimensional
 

Summary

 
In this article, we learned about indexers in C# including the basics of indexers and how to use them.