C#索引器Week例子

来源:互联网 发布:ucm指标源码 编辑:程序博客网 时间:2024/06/06 16:37

---------------------

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