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 *

Comments

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

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