Is it possible to have a static indexer in C#?
Indexer notation requires a reference to this. Since static methods don't have an reference to a particular instance of the class, you can't use this with them, and consequently you can't use indexer notation on static methods
Example of an indexer:
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("{0}\n,{1}\n,{2}\n,{3}\n,{4}\n", obj[0], obj[1], obj[2], obj[3], obj[4]);
}
}