Indexers

Indexers allow you to access an element of a private array or collection field. When you indicate an index for an instance of a class, indexers will get the right data indicated by the index. The following program shows an example of an indexer:

using System;
 
namespace IndexersDemo
{
    public class Person
    {
        private string[] friends;
 
        //Constructor to add friends
        public Person(params string[] friends)
        {
            this.friends = friends;
        }
        
        //This is the indexer
        public string this[int index]         
        {                                     
            get { return friends[index]; }    
            set { friends[index] = value; }   
        }                                     
    }
 
    public class Program
    {
        public static void Main()
        {
            //Create a new person and add some friends
            Person person = new Person("Jenny", "Robert", "Susan", "Charles", "Mandy");
 
            for (int i = 0; i < 5; i++)
            {
                //You can now access each element of the 
                //  private field by specifying the index
                Console.WriteLine(person[i]);
            }
 
            //Changing the second element to John
            person[1] = "John";
        }
    }
}

Example 1 – Indexers Demo

Jenny
Robert
Susan
Charles
Mandy

We created a class containing a private array that will store the friends of the  Person instance (line 7). We also defined a   constructor that will accept string values and assign them to the  friends field (lines 10-13). We defined an indexer  (lines 16-20) for the specified class. The indexer is just like a property, but instead of indicating a name, we used the this keyword and a parameter that will determine the index. We then provide the setters and getters. Inside the getter, we return the array element specified by the value of index. Inside the setter, we assigned the value to the element indicated by the index.

We then created an instance of that class and gave some values as friends of the  Person object (line 28). We queried all the elements of the   private friends field of the instance using a for loop  (lines 30-35). Notice that we didn’t indicate any properties to get the values.   We simply call the value of the element by specifying the index inside square   brackets next to the name of the instance just like how we access array   elements. You can also use the same referencing technique when assigning values   to private array or collection fields. Without indexers, then we have to create a public property for the   friends field.

public string[] Friends
{
    get { return friends; }
    set { friends = value; }
}

And you access the private array field by using the above property.

Console.WriteLine(person1.Friends[0]);
person1.Friends[1] = "John";

One disadvantage of indexers is that you can only create one indexer per class. For example, if you have private fields named friends   and classmates, you need to choose which to put indexer among those private fields. It is better to provide an indexer to an   array field that is most useful for the class and then provide public properties   for all the private arrays and collections including the one that has given an indexer.

Note that you can also use string type as the index. This can be used if you are accessing a Dictionary inside your class. Dictionaries will be discussed in later lessons.

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *