find and solve || findandsolve.com
Please wait.....

Indexer in C# with simple example

In the C# programming language indexer allows an object to be indexed such as an array. If you define an indexer for a class, this class behaves similar to a virtual array.

if you define an indexer for a class, The class will behave live a virtual array, and you can access that class instance values without specifying a type or instance member using an array access operator ([]).

In the c# programming language, the indexer is the like as the property, but the only difference is that the indexer will define this keyword along with the parameters and square bracket.


C# Indexer Syntax


[AccessMmodifier] [ReturnType] this[ParameterType ParameterName]
{
   get
   {
     // return specified index value
   }
   set
   {
     // set a value at the specified index
   }
}


If we observe the given above syntax, you defined an indexer the same as properties with get and set accessors, but the only difference is you used this keyword along with the parameter list.


C# Indexer Example


using System;
namespace FindAndSolve
{
   public class UserInfo
    {
       // Declare an array
       private string[] arr = new string[3];
       // Define the indexer
       public string this[int i]
       {
          get { return arr[i]; }
          set { arr[i] = value; }
       }
    }
    public class Program
    {
      public static void Main(string[] args)
       {
          UserInfo ulist = new UserInfo();
          ulist[0] = "Sylvia Nepal";
          ulist[1] = "Rahul Sharma";
          ulist[2] = "Sundar Neupane";
          for (int i = 0; i < 3; i++)
          {
             Console.WriteLine(ulist[i]);
          }
          Console.ReadLine();
       }
    }
}


Output

Sylvia Nepal

Rahul Sharma

Sundar Neupane


As you see given the above example, we implemented an indexer in the UserInfo class to access the internal collection of a string array. We are using the UserInfo class object ulist as an array to add or retrieve data.


C# Overload Indexer


In the c# programming language, you can overload an indexer by creating other indexers with different index types. The given below example of overloading an indexer with different index types.

using System;
namespace FindAndSolve
{
   public class UserInfo
    {
       // Declare an array
       private string[] array = new string[3];
       // Define the indexer
       public string this[int i]
       {
          get { return array[i]; }
          set { array[i] = value; }
       }
       // Overload an indexer with string type
       public string this[string name]
       {
         get
         {
            foreach (string str in array)
            {
              if (str.ToLower() == name.ToLower())
              {
                 return str.ToUpper();
              }
            }
            return null;
         }
       }
    }
   public class Program
    {
       public static void Main(string[] args)
       {
          UserInfo ulist = new UserInfo();
          ulist[0] = "Sylvia Neupane";
          ulist[1] = "Rahul Sharma";
          ulist[2] = "Sundar Neupane";
          // Accessing Indexer with int type
          Console.WriteLine("*****Indexer with int Type*****");
          for (int i = 0; i < 3; i++)
          {
             Console.WriteLine(ulist[i]);
          }
          // Accessing Indexer with string type
          Console.WriteLine("*****Indexer with string Type*****");
          Console.WriteLine(ulist["Sylvia Neupane"]);
          Console.WriteLine(ulist["Rahul Sharma"]);
          Console.WriteLine(ulist["Sundar Neupane"]);
          Console.ReadLine();
       }
    }
}


Output

*****Indexer with int Type*****

Sylvia Neupane

Rahul Sharma

Sundar Neupane

*****Indexer with string Type*****

SYLVIA NEUPANE

RAHUL SHARMA

SUNDAR NEUPANE


C# Indexer Overview

  • In c# programming language, the indexer is a special type of property, allowing instances of a class or structure to be indexed the same as an array.
  • In c# programming language, the indexer is the same as the property, but the only difference is, the indexer will define with this keyword along with the square bracket and parameters.
  • Indexers can be overloaded by having different signatures.
  • Indexers cannot be static members as it’s an instance of members of the class.
  • Passing indexer value as a ref or out parameter is not supported.