类 建索引

    class Indexes
    {
        public List<string> strList { get; set; }

        public Indexes(List<string> list)
        {
            strList = list;
        }

        public string this[int i]
        {
            get { return strList[i]; }
        }
        public string this[string str]
        {
            get { return strList.FirstOrDefault(x => x == str); }
        }
    }

应用:

string s = "";
            List<string> list = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                list.Add("这是"+i);
            }
            Indexes indexes = new Indexes(list);

            s += indexes[4]+"\t";
            s += indexes["这是5"];

            Console.WriteLine(s);
            Console.ReadKey();

完!

 

posted @ 2018-01-12 08:55  冲天小肥牛  阅读(84)  评论(0编辑  收藏  举报