Can we have statid indexers in C#?

 Posted by Ddd on 1/31/2011 | Category: C# Interview questions | Views: 2509 | Points: 40
Answer:

No, indexers are never static in C#.

Indexers always need an object reference to assign or retreive data from arrays
or collections.

example:

class abc
{
int[] arr=new int[2];

//indexer is defined like this
int this[int a]
{
get
{
return arr[a];
}
set
{
arr[a]=value;
}
}
static void Main()
{
abc p=new abc();
//call to indexer's set block.
p[0]=100;

//get block
Console.WriteLine(p[0]);
}

}








}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response