有没有人可以用一些真实的例子来澄清我们在 C# 类中何时必须使用 索引器
而不是 字段
?
public class GradeHolder
{
private int[] counts = new int[100];
public int this[int grade]
{
get { return this.counts[grade];}
set { this.counts[grade] = value; }
}
}
public class GradeHolder
{
private int[] counts = new int[100];
public void SetCount(int index, int grade)
{
counts[index] = grade;
}
public int GetCount(int index)
{
return counts[index];
}
}