C# introduces a new concept Indexer. This is very useful for some situation. Let as discuss something about Indexer.
- Indexer Concept is object act as an array.
- Indexer an object to be indexed in the same way as an array.
- Indexer modifier can be private, public, protected or internal.
- The return type can be any valid C# types.
- Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.
- this [Parameter]
- {
- get
- {
- // Get codes goes here
- }
- set
- {
- // Set codes goes here
- }
- }
For Example
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Indexers
- {
- class ParentClass
- {
- private string[] range = new string[5];
- public string this[int indexrange]
- {
- get
- {
- return range[indexrange];
- }
- set
- {
- range[indexrange] = value;
- }
- }
- }
- /* The Above Class just act as array declaration using this pointer */
- class childclass
- {
- public static void Main()
- {
- ParentClass obj = new ParentClass();
- /* The Above Class ParentClass create one object name is obj */
- obj[0] = "ONE";
- obj[1] = "TWO";
- obj[2] = "THREE";
- obj[3] = "FOUR ";
- obj[4] = "FIVE";
- Console.WriteLine("WELCOME TO C# CORNER HOME PAGE\n");
- Console.WriteLine("\n");
- Console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n,{4}\n", obj[0], obj[1], obj[2], obj[3], obj[4]);
- Console.WriteLine("\n");
- Console.WriteLine("ALS.Senthur Ganesh Ram Kumar\n");
- Console.WriteLine("\n");
- Console.ReadLine();
- }
- }
- }

Farheen ZaheerPosted Nov 6, 2018, 11:13 PM EST
Need example of indexer in multi dimensional array and also with inheritance.
Farheen ZaheerPosted Nov 6, 2018, 11:11 PM EST
Nice Explanation and easy to understand. Very helpful, thanks.
David KiptooPosted Apr 14, 2018, 3:51 AM EDT
Easy to understand. Indexes allow you to use an object as array
Sunil GuptaPosted Feb 5, 2018, 4:23 AM EST
Hey you explained properly, but still unclear what Indexer does. Real use is missing, it seems.
SubashPosted Jun 21, 2017, 5:32 PM EDT
Little difficult in understand the example
Ankur ChaturvediPosted Sep 10, 2015, 2:18 AM EDT
Nice article easy to understanding of concept ..Thanks alot
Upendra Pratap ShahiPosted Jun 11, 2015, 8:53 PM EDT
good..
sarang chadPosted Oct 21, 2013, 7:25 AM EDT
please tel me different types of indexers
Vidhya SagarPosted May 28, 2013, 2:31 AM EDT
A good basic explanation.. :)
mohammed muddinPosted Nov 26, 2010, 8:52 AM EST
ur explanation is good, i hope u continue the same.thank you.
Md Mostafijur RahmanPosted Nov 15, 2008, 8:42 PM EST
simple....