Indexers definition with example in C#.Net


Like arrays, indexers will allow instances of the class or struct to be indexed.


  1. This keyword is useful for defining the indexers. 
  2. Indexers will enable the objects to be indexed in a similar manner to arrays. 
  3. We can overload indexers. 
  4. We can use Indexers for more than one parameters, in the case of two dimensional array. 

We will go through program now. In the following program we had one generic class which is having just get and set accessor methods for retrieving and assigning values. ClassIndexers class will create an instrance of this class for storing the string values.

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

namespace indexers
    class Program
        static void Main(string[] args)
            //declaring the instance of the exampleCollection type
            exampleCollection<string> objStringCollection = new exampleCollection<string>();

            //using [] notation on the object type.
            objStringCollection[0] = "Ranga Rajesh Kumar";

    class exampleCollection<str>
        //declaring the array for storing the data elements. 
        private str[] arra = new str[100];

        //defining or creating an indexer- which allows client code
        // to use[] notation on the class instance itself.
        public str this[int number]
            get { return arra[number]; }
            set {arra[number]=value; }

Ranga Rajesh Kumar

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar