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 the 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 indexers 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 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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Indexer
- {
- class Program
- {
- static void Main(string[] args)
- {
- indexerExample obj = new indexerExample(5);
- int x;
- obj[0] = 45;
- obj[1] = 3;
- obj[2] = 12;
- obj[3] = 44;
- obj[4] = 4;
- for (x = 0; x < 5; x++)
- {
- Console.WriteLine(obj[x]);
- }
- Console.ReadKey();
-
- }
- }
- public class indexerExample
- {
- int[] arr;
- public indexerExample(int arrSize)
- {
- arr = new int[arrSize];
- }
- public int this[int index_no]
- {
- get { return index_no; }
- set { index_no = value; }
- }
- }
- }
Indexes do not require an underlying array
There is no requirement that an indexe actually operates on an array. It simply must provides functionality that appears array-like to the user for the indexes. Let's understand it with an example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace workSir
- {
- class PwrOfTwo
- {
- public int this[int index]
- {
- get
- {
- if ((index >= 0) && (index < 16))
- return pwr(index);
- else
- return -1;
- }
-
- }
- int pwr(int p)
- {
- int result = 1;
- for (int i = 0; i < p; i++)
- {
- result = result * 2;
- }
- return result;
- }
-
-
- class Program
- {
- static void Main(string[] args)
- {
- PwrOfTwo pwr = new PwrOfTwo();
- Console.WriteLine("power 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();
- Console.ReadKey();
- }
- }
- }
- }
Multi-dimensional indexer
We can create a multi-dimensional array. Let us understand it with the help of an example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Indexer
- {
- class Program
- {
- static void Main(string[] args)
- {
- indexerExample obj = new indexerExample(5,3);
- int x;
-
- obj[0,0]=25;
- obj[0,1]=22;
- obj[0,2]=3;
-
- obj[1,0]=14;
- obj[1,1]=28;
- obj[1,2]=33;
-
- obj[2,0]=3;
- obj[2,1]=92;
- obj[2,2]=64;
-
- obj[3,0]=30;
- obj[3,1]=66;
- obj[3,2]=55;
-
- obj[4,0]=88;
- obj[4,1]=20;
- obj[4,2]=99;
-
-
- for (int i = 0; i < 5;i++ )
- {
- for (x = 0; x < 3; x++)
- {
- Console.Write(obj[i,x] +"\t");
-
- }
- Console.WriteLine();
- }
- Console.ReadKey();
-
- }
- }
- public class indexerExample
- {
- int[,] arr;
- int row, column;
- public indexerExample(int r, int c)
- {
- row = r;
- column = c;
-
- arr = new int[row,column];
- }
- public int this[int index1, int index2]
- {
- get { return arr[index1,index2]; }
- set { arr[index1, index2] = value; }
- }
- }
- }
Summary
In this article we learned about indexers in C# including the basics of indexers and how to use them.