Indexers in C#


C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C# community. Defining a C# indexer is much like defining properties. We can say that an indexer is a member that enables an object to be indexed in the same way as an array.

Properties are used for providing access to scalar values of a class like int, float, string etc. Whereas indexers are used for providing access to a value of an array outside the class.

An indexer is defined the same as a property but we don't have any name to as indexers. As it refer to our own class in the place of name we use "this".

Syntax:

[Modifiers] <type> this[<parameters >]
{
[get {<stmts>;}] //get Accessor
[set {<stmts>;}] //set Accessor

}

Where the modifier can be private, public, protected or internal. The return type can be any valid C# types. The 'this' is a special keyword in C# to indicate the object of the current class. The formal-argument-list specifies the parameters of the indexer. The formal parameter list of an indexer corresponds to that of a method, except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted. Remember that indexers in C# must have at least one parameter. Other wise the compiler will generate a compilation error.

The following program shows a C# indexer in action

Add a new project with the name IndexerDemo; create class indexer IndexerDemo.cs and give it the following code:


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
 
namespace
IndexerDemo
{
    class
IndexerDemo
    {
        string[] arr;
        public IndexerDemo(int Size)
        {
           
//Initializing the array under Constructor
            arr = new strint[Size];

 
           
//Assigning Default value to array
            for (int i = 0; i < Size; i++)
           
                arr[i] = "Empty";
           
        }

 
       
//Defining an Indexer to access the values of array which is private outside the class.


        public string this[int index]
        {
            get { return arr[index]; }
            set { arr[index] = value; }
        }
    }
}

Now to access the array using the indexer, write the following code into your main program class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IndexerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            IndexerDemo obj = new IndexerDemo(6);
            //Getting the defaulat value of array
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(obj[i]);
            }
            Console.WriteLine();
            //Setting the Array with new Values:

            obj[0] = "India";
            obj[1] = "America";
            obj[2] = "Australia";
            obj[3] = "England";

            //Getting the current values of array:

            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(obj[i]);
            }
            Console.ReadLine();

        }
    }
}


Run Your Project.
 

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all