How to Create Indexer in C#

Indexer

Indexer permits members of an instance of a class or struct to be indexed in the same way as elements of array. Indexer are similar to properties except that the name of indexer is this followed by a parameter list.

Syntax : <modifier> <return-type> this [formal argument list]

The modifier can be private, public, protected or internal. Indexer has get and set blocks. The formal argument list specifies the parameters of the index. A get accessor returns value and set accessor assigns a value. The value keyword is used to define the value being assigned by the set indexer. C# do not have the concept of static indexers. Indexer can be overloaded, A class can have more than one indexer with different signatures. Indexer do not have to be indexed by an integer value only, it is up to you how to define the specific look-up mechanism. A base class indexer is inherited to the derived class, It can be overridden in the child class. It is accessed through in index.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

 

namespace MyTestConsole.DemoClasses

{

    public class Employee

    {

        #region Indexer

 

        private int[] arr = new int[100]; //indexer

        private char[] chr = new char[10];//indexer

 

        public int this[int index] // Syntax of indexer

        {

            get

            {

                if (index < 0 || index >= 100)

                {

                    return 0;

                }

                else

                {

                    return arr[index];

                }

            }

            set

            {

                if (!(index < 0 || index >= 100))

                {

                    arr[index] = value;

                }

            }

        }

 

        public char this[char ind]

        {

            set

            {

                chr[ind] = value;

            }

            get

            {

                return chr[ind];

            }

        }

 

        #endregion

    }

}
 

using MyTestConsole.DemoClasses;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

 

 

namespace MyTestConsole

{

    class Program

    {

        static void Main()

        {

            Console.WriteLine("!! My First Console App !!");

            Console.WriteLine();

 

            #region Indexer

 

            Console.WriteLine("Indexer for int");

            Employee emp = new Employee();

            emp[2] = 20;                  //set value to indexer

            emp[5] = 98;

            for (int i = 0; i <= 10; i++)

            {

                Console.WriteLine("{0} : {1} ", i, emp[i]); //get value

            }

 

            //for char

            Console.WriteLine();

            Console.WriteLine("Indexer for char");

            Employee chr = new Employee();

            chr[0] = 'J';

            chr[1] = 'e';

            chr[2] = 'e';

            chr[3] = 't';

            chr[4] = 'e';

            chr[5] = 'n';

            chr[6] = 'd';

            chr[7] = 'r';

            chr[8] = 'a';

            for (int j = 0; j < 9; j++)

            {

                Console.WriteLine("{0} : {1} ", j, Convert.ToChar(chr[j]));

            }

 

            #endregion

        }

    }

}