Learn Programming

C# Indexers

What is an indexer?

An indexer in C# is something that you can use to make an object work like an array. In other words it lets you use a number in square brackets after an object to access data in the object.

When are indexers used?

There aren't a lot of places where an indexer can be used but when you find one they can save a bit of code and make things much easier to understand. Any object that has a .GetValueAtPosition() type of method will benefit from the use of an indexer. The System.Data.DataRow class that comes with .Net is a good example of how an indexer is used to access the data items in the DataRow.

An example

As an example we will be making our own data row. The row will contain columns of data in it which will be each individually accessible through the use of the indexer. We are then going to make a simple console application to test the row class that we make.

First create a class called Row and save it as Row.cs. Here is the normal empty class code for our Row class:

using System;

public class Row
{
}

We will add a List of objects to the Row to contain the data.

using System;
using System.Collections.Generic;

public class Row
{
   public List<object> Data = new List<object>();
}

Now we will add the indexer code.

using System;
using System.Collections.Generic;

public class Row
{
   public List<object> Data = new List<object>();

   public object this[int ColumnIndex]
   {
      get
      {
         return Data[ColumnIndex];
      }
      set
      {
         Data[ColumnIndex] = value;
      }
   }

}

You will notice that the indexer code looks almost exactly like the code for a property which means that it should be easy for you to understand how it works. The indexer uses the this keyword to show that it is an indexer and not a property. It has a return value just like a property or method and I have used object as the return type here because a row can contain data of many different types. The square brackets behind this contain the value of the item to find which can be of any type but is most often an integer or a string. You then have the get and set accessors in which the code is written to get the value for the index that comes through in the index parameter.

Now we need to create a class to test the Row indexer called Program which must be saved as Program.cs. Here is the code:

using System;

public class Program
{
   public static void Main(string[] args)
   {
      Row EmployeeRow = new Row();

      EmployeeRow.Data.Add(1);
      EmployeeRow.Data.Add("John");
      EmployeeRow.Data.Add("Smith");

      Console.WriteLine(EmployeeRow[0]);
      Console.WriteLine(EmployeeRow[1]);
      Console.WriteLine(EmployeeRow[2]);

      Console.ReadLine();
   }
}

The program starts off by instantiating the Row class with the name EmployeeRow because we are going to be storing employee data in the row in this example. 3 values are then added to the row for the employee number, first name and last name. Next is the part where the indexer is used. The values in EmployeeRow are printed out by using the column position of the data in the row as the index value.