Indexer
What is Indexer ??
• C# indexers are usually known as smart arrays.
• A C# indexer is a class property that allows you to access
a member variable of a class or struct using the same way
as array.
• In C#, indexers are created using this keyword.
• Indexers in C# are applicable on both classes and structs.
Important points to remember on indexers:
• Indexers are always created with this keyword.
• Parameterized property are called indexer.
• Indexers are implemented through get and set accessors for the [ ] operator.
• The value keyword is used to define the value being assigned by the set
indexer.
• ref and out parameter modifiers are not permitted in indexer.
• A get accessor return a value. A set accessor assigns a vlaue.
• Indexer is an instance member so can't be static but property can be
static.
• Indexers are used on group of elements.
• Indexer is identified by its signature where as a property is identified
it's name.
• Indexers are accessed using indexes where as properties are
accessed by names.
• Indexer can be overloaded.
• There are two types of Indexers i.e. One Dimensional Indexer &
MultiDimensional Indexer. The above discussed is One Dimensional
Indexer.
• Syntax:
[access_modifier] [return_type] this [argument_list]
{
get { // get block code }
set { // set block code }
}
• access_modifier: It can be public, private, protected or
internal.
• return_type: It can be any valid C# type.
• this: It is the keyword which points to the object of the
current class.
• argument_list: This specifies the parameter list of the
indexer.
• get{ } and set { }: These are the accessors.
Example
using System; class main
class IndexerCreation {
{ public static void Main()
private string[] val = new string[3]; {
public string this[int index] IndexerCreation ic = new IndexerCreation();
{ ic[0] = "C";
get ic[1] = "CPP";
{ ic[2] = "CSHARP";
return val[index]; Console.Write("Printing values stored in
objects used as arrays\n");
}
Console.WriteLine("First value = "+ ic[0]);
set
Console.WriteLine("Second value = "+ ic[1]);
{ Console.WriteLine("Third value = "+ic[2]);
val[index] = value; }
} }
}
}
Overloaded Indexers
• Indexers can be overloaded.
• Indexers can also be declared with multiple
parameters and each parameter may be a
different type.
• It is not necessary that the indexes have to be
integers.
• C# allows indexes to be of other types, for
example, a string.
Example
using System;
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;}
}
}
public int this[string name]
{
get
{
int index = 0;
while(index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
}
}
static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[0] = "Komal";
names[1] = "Heena";
names[2] = "Kajal";
names[3] = "Pooja";
names[4] = "Roshani";
names[5] = "Meera";
names[6] = "Heer";
//using the first indexer with int parameter
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
//using the second indexer with the string parameter
Console.WriteLine(names["Kajal"]);
Console.ReadKey();
}
}