The indexer allows instances of classes and structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that their accessors use parameters. It is called the property with parameters.
Simple indexer instance:
Class Program
{
Static void main (string [] ARGs)
{
Indexclass A = new indexclass ();
A [0] = "James ";
A [1] = "Li Si ";
A [2] = "Wang Wu ";
Console. writeline ("A [0] =" + A [0]);
Console. writeline ("A [1] =" + A [1]);
Console. writeline ("A [2] =" + A [2]);
Console. readkey ();
}
}
Class indexclass
{
Private string [] Name = new string [10];
Public String This [int Index]
{
Get {return name [Index];}
Set {This. name [Index] = value ;}
}
}
Comparison between indexer and array:
The index value of the indexer is not subject to type restrictions. The index value used to access the array must be an integer, and the index can be another type of index value.
The indexer can be overloaded. A class can have multiple indexers.
The indexer does not directly store data for a variable. The indexer has get and set accessors.