当我们必须使用Indexers
而不是Fields
在 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];
}
}