14.1 Indexers in C#
The indexing refers to the position of an element in the respective structure. The indexing is performed using the [ ] operator. The element position is defined in the angular brackets. The looping structure is used for accessing the elements of an array.
Sometimes, user needs to overload the [ ] operator for a class that user has created. The operator method is not used for overloading the [ ] operator. The indexers are used for overloading the [ ] operator. An indexer allows an object to be indexed like an array. The indexers are useful to support the creation of specialized arrays respective to one or more constraints.
Advantages of Indexers:
1) They are used for overloading an [ ] operator
2) The syntax is simple an easy to user
3) It supports overloading for any user defines array in C#
4) The access specifiers simply the code complexity
14.2 Implementation of Indexers
element-type this [ int index ] { //The get accessor { //return the value specified by index } //The set accessor { //set the value specified by index } }
An indexer can be implemented for any array defined in the application. The general syntax of indexers in C# is as shown below:
In the syntax of indexers, the element type is used for defining the access specifiers in the code. The position or the size of an array is defined in the [ ] operator. The get access specifier is used to retrieve the values for the property. The set access specifer is used to assign the values to the element in an array.
A sample code to demonstrate the use of Indexers in C# is as shown below:
class Numbers { string[ ] values = new string [20]; public string this [ int number ] { get { if ( number >=0 && number <values.Length ) { return values [number]; } return “Invalid”; } set { if ( number >=0 && number <values.Length ) { values [ number ] = value; } } } } class Program { static void Main ( string[ ] args ) { Numbers n = new Numbers(); string result, result1, result2; n[1] = “Harry”; n[3] = “John”; n[9] = “Peter”; n[100] = “Invalid”; result = n[1]; result1 = n[3]; result2 = n[100]; Console.WriteLine(result); Console.WriteLine(result2); Console.WriteLine(result3); Console.Read(); } }
The output for the code is as shown below:
In the above code, the Numbers class is included with an indexer. The class contains an indexer with the get and set methods. They contain the code for checking the array index is not out of bounds in the application.
In the Main() method, user assigns the elements in the Numbers class. Various literals are stored in it. The elements are used as parameters but not for changing the backing store of the literals.
14.3 Overloading Indexers
Indexers in C# can be overloaded. The parameters defined are multiple and have different data type. The data type of the parameters can be integer, string, float or any other. User can define different indexers in an application. Each indexer has a different data type by overloading it.
class OverloadIndex { string[ ] data; int arrsize; public OverloadIndex (int size) { arrsize = size; data = new string [size]; for(int i=0; i<size; i++) { data[i] = “null”; } } public string this [ int loc ] { get { return data[loc]; } set { data[loc] = value; } } public string this[ string newdata ] { get { int c= 0; for ( int i=0; i<arrsize; i++) { if ( data[i]==newdata) { c++; } } return c.ToString(); } set { for ( int i=0; i<arrsize; i++) { if(data[i] ==newdata) { data[i] = value; } } } } static void Main ( string[ ] args ) { int size = 20; OverloadIndex o = new OverloadIndex(size); o[2] =”Sam”; o[10] = “John”; o[“empty”] = “no value”; Console.WriteLine(“The output is”); for (int i = 0;i<size;i++) { Console.WriteLine(“o[{0}]:{1}”, i, o[i]); } Console.Read(); } }
The output for the code is as shown below:
In the above code, the first indexer contains the int parameter as loc. It contains a new indexer with string parameter. The get accessor of the indexer returns the string representation containing the number of items that matches the parameter value.
The set accessor modifies the entry of the array that matches the data parameter to the value assigned to the indexer. The overloaded indexer takes a string parameter in the Main() method of the code. It invokes the set accessor for assigning the value as null where the value is empty.
14.4 Implementation of Multi – Parameter Indexers
The indexers in C# can take more than one parameter. User can create an indexer property having more than one dimension. The indexer signature is specified by the number and type of parameters in the list. An indexer with multiple parameters would implement as shown below:
public object this [ int param1, …, int paramN ] { get { //processes and returns the data } set { //processes and assigns the value } }
The steps for creating the multi parameter index are as mentioned below:
1) Declare an array in an application
2) In the body of the code, add the get and set accessor
3) After the property is created, each element of the array by adding the square brackets to an instance of the class
The code sample for multi parameters indexers in C# is as shown below:
class Matrix { double [ , ] a; public double this [ int x, int y ] { get { return a[ x, y]; } } public Matrix() { a = new double [2,4]; a[0,0] = 12.24; a[0,1] = 14.22; a[0,2] = 16.76; a[0,3] = 20.10; a[1,0] = 30.10; a[1,1] = 40.10; a[1,2] = 32.80; a[1,3] = 15.55; } } public class Program { static void Main ( string[] args ) { Matrix m = new Matrix(); for ( int i=0; i<2; i++ ) { for ( int j=0;j<2;j++) { double value = a[i,j]; Console.WriteLine(“Number is [{0}] [{1}]:{2}”, i, j, value ); } Console.WriteLine(); Console.Read(); } } }
The output for the code is as shown below:
In the above code, an array ‘a’ of type double is created in an application. The accessors are added in the code. The values for the array are added in the constructor of the class. The values are extracted in Main() method of an application.