top

Search

C# Tutorial

Indexers in C# allow the objects of classes or structures to be indexed like arrays. Classes behave like virtual arrays when indexers are defined for them.Some of the salient points about indexers are as follows:The “this” keyword is used for the creation of indexers.The get and set accessors are used to implement indexers.The indexers can be overloaded.The indexers cannot be static as they are an instance member but the properties can be static.The ref and out parameter modifiers are not permitted in indexer.There should be at least one parameter in indexes otherwise an error occurs.Indexer SyntaxThe syntax for indexers is given as follows:Modifier ReturnType this [ArgumentList] {    get    {          // The code in get block    }    set    {          // The code in set block     } }In the above syntax, this keyword is used to create the indexer. The Modifier can be private, protected, public or internal while the ReturnType is any C# data type.The ArgumentList specifies the indexer arguments. Then the get and set accessors are used to implement the indexer.Indexer ProgramA program that demonstrates indexers in C# is given as follows:Source Code: Program that demonstrates indexers in C#using System;   namespace IndexerDemo {      class Example      {        class IndexerFruitClass          {            private string[] fruit_names = new string[5];              public string this[int i]              {                get                {                    return fruit_names[i];                  }                set                {                    fruit_names[i] = value;                  }            }        }        static void Main(string[] args)          {            IndexerFruitClass Fruit = new IndexerFruitClass();              Fruit[0] = "Apple";              Fruit[1] = "Guava";            Fruit[2] = "Mango";            Fruit[3] = "Grapes";            Fruit[4] = "Melon";            Console.WriteLine("Some fruit names are as follows:");              for (int i = 0; i < 5; i++)              {                Console.WriteLine(Fruit[i]);              }        }    } }The output of the above program is as follows:Some fruit names are as follows: Apple Guava Mango Grapes MelonNow let us understand the above program.The class IndexerFruitClass contains a string fruit_names. Then, this keyword is used to create the indexer where the get accessor returns the fruit_names[i] and the set accessor sets the value of fruit_names[i]. The code snippet for this is given as follows:class IndexerFruitClass          {            private string[] fruit_names = new string[5];              public string this[int i]              {                get                {                    return fruit_names[i];                  }                set                {                    fruit_names[i] = value;                  }            }        }In the Main() function, an object of the IndexerFruitClass is declared i.e. Fruit. All the Fruit values are initialized and finally they are displayed using a for loop. The code snippet for this is given as follows:static void Main(string[] args)          {            IndexerFruitClass Fruit = new IndexerFruitClass();              Fruit[0] = "Apple";              Fruit[1] = "Guava";            Fruit[2] = "Mango";            Fruit[3] = "Grapes";            Fruit[4] = "Melon";            Console.WriteLine("Some fruit names are as follows:");              for (int i = 0; i < 5; i++)              {                Console.WriteLine(Fruit[i]);              }        }
logo

C# Tutorial

Indexers in C#

Indexers in C# allow the objects of classes or structures to be indexed like arrays. Classes behave like virtual arrays when indexers are defined for them.

Some of the salient points about indexers are as follows:

  1. The “this” keyword is used for the creation of indexers.
  2. The get and set accessors are used to implement indexers.
  3. The indexers can be overloaded.
  4. The indexers cannot be static as they are an instance member but the properties can be static.
  5. The ref and out parameter modifiers are not permitted in indexer.
  6. There should be at least one parameter in indexes otherwise an error occurs.

Indexer Syntax

The syntax for indexers is given as follows:

Modifier ReturnType this [ArgumentList]
{
   get
   {
         // The code in get block
   }
   set
   {
         // The code in set block
    }
}

In the above syntax, this keyword is used to create the indexer. The Modifier can be private, protected, public or internal while the ReturnType is any C# data type.The ArgumentList specifies the indexer arguments. Then the get and set accessors are used to implement the indexer.

Indexer Program

A program that demonstrates indexers in C# is given as follows:

Source Code: Program that demonstrates indexers in C#

using System;  
namespace IndexerDemo
{  
   class Example  
   {
       class IndexerFruitClass  
       {
           private string[] fruit_names = new string[5];  
           public string this[int i]  
           {
               get
               {
                   return fruit_names[i];  
               }
               set
               {
                   fruit_names[i] = value;  
               }
           }
       }
       static void Main(string[] args)  
       {
           IndexerFruitClass Fruit = new IndexerFruitClass();  
           Fruit[0] = "Apple";  
           Fruit[1] = "Guava";
           Fruit[2] = "Mango";
           Fruit[3] = "Grapes";
           Fruit[4] = "Melon";
           Console.WriteLine("Some fruit names are as follows:");  
           for (int i = 0; i < 5; i++)  
           {
               Console.WriteLine(Fruit[i]);  
           }
       }
   }
}

The output of the above program is as follows:

Some fruit names are as follows:
Apple
Guava
Mango
Grapes
Melon

Now let us understand the above program.

The class IndexerFruitClass contains a string fruit_names. Then, this keyword is used to create the indexer where the get accessor returns the fruit_names[i] and the set accessor sets the value of fruit_names[i]. The code snippet for this is given as follows:

class IndexerFruitClass  
       {
           private string[] fruit_names = new string[5];  
           public string this[int i]  
           {
               get
               {
                   return fruit_names[i];  
               }
               set
               {
                   fruit_names[i] = value;  
               }
           }
       }

In the Main() function, an object of the IndexerFruitClass is declared i.e. Fruit. All the Fruit values are initialized and finally they are displayed using a for loop. The code snippet for this is given as follows:

static void Main(string[] args)  
       {
           IndexerFruitClass Fruit = new IndexerFruitClass();  
           Fruit[0] = "Apple";  
           Fruit[1] = "Guava";
           Fruit[2] = "Mango";
           Fruit[3] = "Grapes";
           Fruit[4] = "Melon";
           Console.WriteLine("Some fruit names are as follows:");  
           for (int i = 0; i < 5; i++)  
           {
               Console.WriteLine(Fruit[i]);  
           }
       }

Leave a Reply

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

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial