Indexer using C#

Indexed properties or indexers, allows „array-like access to groups of items“. In other words, if a class makes use of array or other collection types, it is recommended to use indexers for accessing the values of these internal collections. Whereas the standard C# Properties are used to access single values in classes, an indexed property is used to encapsulate a set of values.

Other important things to remember about Indexers are:

 

  • They are identified through the use of the keyword this.

  • Indexed properties can be created in classes or structs.

  • Only one indexed property can exist in a single class or struct. However, Indexers can be overloaded (just like methods), whereas arrays cannot.

  1. public EmployeeNumber this [PhoneNumber number] { ………………. }  
  2.   
  3. public PhoneNumber this [ EmployeeNumber number] { ……………..…} 
  • Indexers can use non-numeric subscripts, such as string as shown below whereas Arrays can use only integer subscripts.
  1. public int this [string id ] {……………..} //OK. 

  • They can contain get and set methods just like other properties.
  • Indexers cannot be used as ref or out parameters, whereas array elements can:
  1. EmployeeIndexer empIndexer ;// empIndexer contains an indexer  
  2.   
  3. GetEmpId(ref empIndexer[1]) ; // Compile time error. 
  • They accept an index value in place of the standard property value parameter.

Let’s understand the whole new concept with an example.

We have a list of Person. Person class contains Name and ID. Indexer as highlighted in the code snippet below expects a Name to be looked in to the list of persons and it returns the ID the person found.

  1.   public class Person  
  2.   {  
  3.       #region member variable  
  4.         
  5.       private Person[] _listOfPersons;  
  6.   
  7.       private string _name;  
  8.   
  9.       private int _id;  
  10.  
  11.       #endregion  
  12.       #region member function  
  13.         
  14.       /// <summary>  
  15.       /// get the list of the person  
  16.       /// this list may also come from the database  
  17.       /// </summary>  
  18.       public void GetPersonList()  
  19.       {  
  20.           _listOfPersons = new Person[] { new Person() { _id = 1, _name = "James" }, new Person() { _id = 2, _name = "Kevin" } };  
  21.       }  
  22.       #endregion  
  23.       #region indexer  
  24.   
  25.       public int this[string name]  
  26.       {  
  27.           get { return (from a in _listOfPersons where String.Equals(a._name, name) select a._id).FirstOrDefault(); }  
  28.       }  
  29.  
  30.       #endregion  
  31.   } 
Some points which need attention are:

To define the indexer, a notation that is cross between a property and an array is used. Indexers are defined with the this keyword and type of the value returned by the indexer. And the most important is to specify the type of the value to use as the index into the indexer between square brackets.

To use retrieve a value from a collection, one needs to pass the value that need to be matched in the collection. There gives different ways to look in to the collection but in my case I have kept it simple and used a LINQ statement.

The code has been attached for the reference.