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:
Example
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace PracticeIndexer
- {
- public class clsPerson
- {
- public string Name { get; set; }
- public clsPerson(string name)
- {
- Name = name;
- }
- }
-
- class clsPersonIndexer
- {
- private clsPerson[] myData;
-
- public clsPersonIndexer(int size)
- {
- myData = new clsPerson[size];
-
- for (int i = 0; i < size; i++)
- {
- myData[i] = new clsPerson(string.Empty);
- }
- }
- public string this[int pos]
- {
- get
- {
- return "Hi " + myData[pos].Name + "..";
- }
- set
- {
- myData[pos].Name = value;
- }
- }
-
- }
- class Program
- {
- static void Main(string[] args)
- {
- int size = 10;
- clsPersonIndexer myValue = new clsPersonIndexer(size);
- myValue[1] = "ratnesh";
- myValue[7] = "swati";
- myValue[5] = "karunesh";
- Console.WriteLine("\nIndexer Output\n");
- for (int i = 0; i < size; i++)
- {
- Console.WriteLine(myValue[i]);
- }
- Console.WriteLine("\nPress enter to exit..\n");
- Console.ReadLine();
-
- }
- }
- }
OutputIn 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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace PracticeIndexer
- {
- public class clsPerson
- {
- public string Name { get; set; }
- public int Score { get; set; }
- public clsPerson(string name, int score)
- {
- Name = name;
- Score = score;
- }
- }
-
- class clsPersonIndexer
- {
- private clsPerson[] myData;
-
- public clsPersonIndexer(int size)
- {
- myData = new clsPerson[size];
-
- for (int i = 0; i < size; i++)
- {
- myData[i] = new clsPerson(string.Empty,0);
- }
- }
- public string this[int pos]
- {
- get
- {
- return "Hi " + myData[pos].Name + "..";
- }
- set
- {
- Random rnd = new Random();
- myData[pos].Name = value;
- myData[pos].Score= Convert.ToInt16( DateTime.Now.Ticks%100);
- }
- }
-
- public string this[int pos, int marks]
- {
- get
- {
- string info = "";
- if (myData[pos].Name != string.Empty)
- {
- if (myData[pos].Score > marks)
- {
- info = "Hi " + myData[pos].Name + "(Score: " + myData[pos].Score + "), you are qualified..";
- }
- else
- {
- info = "Hi " + myData[pos].Name + "(Score: " + myData[pos].Score + "), you are not qualified..";
- }
- }
- return info;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int size = 10;
- clsPersonIndexer myValue = new clsPersonIndexer(size);
- myValue[1] = "ratnesh";
- myValue[7] = "swati";
- myValue[5] = "karunesh";
- Console.WriteLine("\n ...Indexer Output...\n");
- for (int i = 0; i < size; i++)
- {
- Console.WriteLine(myValue[i]);
- }
- Console.WriteLine("\n ...Overloaded Indexer Output...\n");
-
- for (int i = 0; i < size; i++)
- {
- string info=myValue[i,60];
- if (info != string.Empty)
- Console.WriteLine(info);
- }
-
- Console.WriteLine("\n ...Press enter to exit...\n");
- Console.ReadLine();
-
- }
- }
- }