一个索引使用范例不是很明白
程序代码:
class DayCollection//声明一个存储星期几的类 { //定义字符串数组days用以存储星期信息 string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" }; //方法GetDay用于根据字符串查找星期几,找不到返回-1 private int GetDay(string testDay) { int i = 0; foreach (string day in days) {//遍历查询testDay在数组中的索引 if (day == testDay) { return i; } i++; } return -1; } public int this[string day] { get { return (GetDay(day)); }//调用GetDay方法得到day在数组中的索引并返回 } } class Program { static void Main(string[] args) { //创建DayCollection累对象week DayCollection week = new DayCollection(); //利用索引器查询“Fri”在days数组中对应的索引 System.Console.WriteLine(week["Fri"]); //无效的查询返回-1 System.Console.WriteLine(week["InvalidDay"]); Console.ReadKey(); } }testDay的参数来源是什么?
if (day == testDay)
{
return i;
}
i++;
}
return -1;
索引器中也没有testDay啊
搞不懂- -麻烦帮忙解释一下