Skip to content

#14 INDEXERS

It allows an instance of a class to be indexed and accessed as an array. When we define an index for a class then we can access the instance of this class using the array access operator([ ]).

[access_modifier] [return_type] this[argument_list]
{
     get
     {
         // get block code
     }
     set
     {
         // set block code
     }
}
using System;

namespace IndexerApplication
{

    class Indexer
    {
        private string[] namelist = new string[size];
        static public int size = 10;

        public Indexer()    //Default constructor used to assign default values but not a mandatory one
        {
            for (int i = 0; i < size; i++)
                namelist[i] = "N. A.";
        }

        public string this[int index]
        {
            get
            {
                if (index >= 0 && index <= size - 1)
                {
                    return namelist[index];
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (index >= 0 && index <= size - 1)
                {
                    namelist[index] = value;
                }
            }
        }

        static void Main(string[] args)
        {
            Indexer names = new Indexer();
            names[0] = "Teja";
            names[1] = "Vamsi";
            names[2] = "Akshay";
            names[3] = "Mona";
            names[4] = "Raj";
            names[5] = "Sunil";
            names[6] = "Rubic";

            for (int i = 0; i < Indexer.size; i++)
            {
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();
        }
    }
}

Important points about indexers:

  1. Indexers can be overloaded.
  2. They look like a property but are different from Properties.
  3. A set accessor will always assign the value while the get accessor will return the value.
  4. “This” keyword is used to declare an indexer.
  5. Indexers are also known as Smart Arrays or Parameterized Property in C#.
  6. Indexers can’t be a static member.

When to Use Indexers:

  1. When we need an array of instances for a class
  2. If your class contains a list/array of values directly related to the class.

Generic Indexer

we can define Indexer to be generic as well

    class Program
    { 
        static void Main(string[] args)
        {
            GenericIndexerclass<string> ptr = new GenericIndexerclass<string>(5);
            ptr[0] = "Tejaswi";
            ptr[1] = "Raja";
            ptr[2] = "Vamsi";
            ptr[3] = "Akshay";
            ptr[4] = "Space";

            for(int i=0; i<ptr.Length; i++)
            {
                Console.WriteLine(ptr[i]);
            }

            
            Console.ReadKey();
        }
    }

    class GenericIndexerclass<T>
    {
        private T[] namelist;

        //you  can specify the length dynamically or hard code it
        public GenericIndexerclass(int length)
        {
            namelist = new T[length];
        }

        public T this[int index]
        {
            get
            {
                if(index<0 && index>namelist.Length)
                {
                    throw new Exception("Index out of range");
                }
                
                return namelist[index];
            }

            set
            {
                if(index<0 && index>namelist.Length)
                {
                    throw new Exception("Indexx out of range");
                }

                namelist[index] = value;
            }
        }

        //This is to expose length property to the class object
        public int Length
        {
            get
            {
                return namelist.Length;
            }
        }
    }
Published inC#

2 Comments

  1. SMS SMS

    Very good article! We are linking to this particularly great content on our site. Keep up the great writing.

  2. Hi to every one, the contents present at this website are
    truly awesome for people knowledge, well, keep up the nice work fellows.

Leave a Reply

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