---------------------
using System;
namespace CSharpBook.Chapter07
{
class DayCollection
{ // 星期(字符串)数组
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
public int this[string index]
{
get {
for (int i = 0; i < days.Length; i++)
{
if (days[i] == index) {
return i;
}
}
return -1;
}
}
}
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
Console.WriteLine("{0}:{1}", "Friday", week["Fri"]);
Console.WriteLine("{0}:{1}", "Unknown", week["Unknown"]);
Console.ReadLine();
}
}
}
输出结果:
friday:5
unknown:-1
C#索引器Week例子
最新推荐文章于 2025-08-17 13:44:41 发布