SoFunction
Updated on 2025-03-06

Tutorial on using C# indexer

Overview

Indexer allows an object to be accessed in the same way as an array.

When you define an indexer for a class, the class behaves like a virtual array. You can use the array access operator [ ] to access members of the class.

grammar

The syntax of a one-dimensional indexer is as follows:

element-type this[int index]
{
   //  get Accessor   get
   {
      // Return the value specified by index   }

   //  set Accessor   set
   {
      // Set the specified value of index   }
}

The purpose of indexer

The declaration of the behavior of an indexer is somewhat similar to a property. Just like properties, you can use get and set accessors to define indexers. However, the property returns or sets a specific data member, while the indexer returns or sets a specific value of the object instance. In other words, it divides the instance data into smaller sections and indexes each section, obtains or sets each section.

Defining a property includes providing the property name. The indexer is defined without a name, but with this keyword, which points to the object instance. The following example demonstrates this concept:

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      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;
            }
         }
      }

      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         for ( int i = 0; i < ; i++ )
         {
            (names[i]);
         }
         ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.

Overload indexer (Indexer)

The indexer can be overloaded. The indexer can also be declared with multiple parameters, and each parameter can be of a different type. There is no need to make the indexer an integer. C# allows the indexer to be of other types, for example, string type.

The following example demonstrates the overload indexer:

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i &lt; size; i++)
         {
          namelist[i] = "N. A.";
         }
      }
      public string this[int index]
      {
         get
         {
            string tmp;

            if( index &gt;= 0 &amp;&amp; index &lt;= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }

            return ( tmp );
         }
         set
         {
            if( index &gt;= 0 &amp;&amp; index &lt;= size-1 )
            {
               namelist[index] = value;
            }
         }
      }
      public int this[string name]
      {
         get
         {
            int index = 0;
            while(index &lt; size)
            {
               if (namelist[index] == name)
               {
                return index;
               }
               index++;
            }
            return index;
         }

      }

      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         // Use the first indexer with the int parameter         for (int i = 0; i &lt; ; i++)
         {
            (names[i]);
         }
         // Use a second indexer with string parameter         (names["Nuha"]);
         ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2

The above is the detailed content of the tutorial on using C# indexer. For more information about the use of C# indexer, please pay attention to my other related articles!