What is meant by an Indexer ?

 Posted by Bharathi Cherukuri on 4/26/2012 | Category: ASP.NET Interview questions | Views: 1719 | Points: 40
Answer:

An indexer is a member which enables an object to be indexed in the same way as an array.

Example:

namespace ConsoleApplication

{
using System;
class Employee
{
private string[]name = new string[10];
public string this[int index]
{
get
{
return name[index];
}
set
{
name[index] = value;
}
}
}

class Test
{
public static void Main()
{
Employee emp = new Employee();
emp[0] = "Joydip";
emp[1] = "Manashi";
emp[2] = "Jini";
Console.WriteLine("The namesare:--");
for (int i = 0; i < 3;Console.WriteLine(emp[i++]))
;
Console.ReadLine();
}
}
}


The output is as follows:

The names are:--

Joydip

Manashi

Jini


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response