Suppose if i have a class like below and if i want to add some types as an list, then i can do this
class student
{
public string name { get; set; }
}
class MainClass
{
static void Main()
{
var lists = new List<student>();
lists.Add(new student { name = "foo" });
lists.Add(new student { name = "mary" });
lists.Add(new student { name = "jane" });
lists[1].name = "moo";
}
}
now, Indexer also does the same, then why should i use indexer over List
class TempRecord
{
// Array of temperature values
private float[] temps = new float[10];
public float this[int index]
{
get
{
return temps[index];
}
set
{
temps[index] = value;
}
}
}
class MainClass
{
static void Main()
{
TempRecord tempRecord = new TempRecord();
// Use the indexer's set accessor
tempRecord[3] = 58.3F;
tempRecord[5] = 60.1F;
}
}
my question is 1. why is indexer needed in the first place when we have List which does the same job 2. is there any clr advantage for using the indexer