C# Indexers

Indexers in C# allow an object to be indexed like an Array. When you define an indexer for a class, this class behaves similar to a virtual array. You can then access the instance of this class using the array access operator ([ ]). In C#, Indexers are known as the special kind of attribute which allows class instances to be indexed like an array.

Syntax:

using System;

 namespace Indexers
{
 public class Students
    {
       // Declare an array
       private string[] arr = new string[3];
       // Defining indexer
       public string this[int i]
       {
          get { return arr[i]; }
          set { arr[i] = value; }
       }
    }
 public class Program
    {
    public static void Main(string[] args)
       {
          Students list = new Students();
          list[0] = "TOM";
          list[1] = "JOOHN";
          list[2] = "ALICE";
          for (int i = 0; i < 3; i++)
          {
             Console.WriteLine(list[i]);
          }
          Console.ReadLine();
       }
    }
}

Overloaded Indexers:

There are some overloaded indexers in C# which are created by using indexers with different types. To declare indexers we can use multiple parameters with different types.

using System;

namespace CsharpIndexer {
  public class IndexedNames {
      private string[] namelist = new string[size];
      static public int size = 5;
      
      public IndexedNames() {
         for (int i = 0; i < size; i++) {
            namelist[i] = "N. A.";
         }
      }
      public string this[int index] {
         get {
            string tmp;
            
            if( index >= 0 && index <= size-1 ) {
               tmp = namelist[index];
            } else {
               tmp = "";
            }
            
            return ( tmp );
         }
         set {
            if( index >= 0 && index <= size-1 ) {
               namelist[index] = value;
            }
         }
      }
      
      public int this[string name] {
         get {
            int index = 0;
            
            while(index < size) {
               if (namelist[index] == name) {
                return index;
               }
               index++;
            }
            return index;
         }
      }

    public static void Main(string[] args) {
         IndexedNames names = new IndexedNames();
         names[0] = "ALICE";
         names[1] = "JOHN";
         names[2] = "TOM";
         names[3] = "ELIC";
         
         //using the first indexer with int parameter
         for (int i = 0; i < IndexedNames.size; i++) {
            Console.WriteLine(names[i]);
         }
         
         //using the second indexer with the string parameter
         Console.WriteLine(names["TOM"]);
          }
  }
}

Indexers Properties:

  1. Indexers in C# allow an object to be indexed like an array.
  2. Indexers is defined with keyword along with the square bracket and parameters.
  3. Indexers are instance members of the class so they can not be any statis member.
  4. If we pass value of the indexers as a ref or out parameter, it doesn’t supported by the system.