C#初始索引器

string[] s = new string[5];

最常见的数组,中括号[ ],通过下标访问,即通过索引访问

关于索引器

    class Person
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        public string this[string index]
        {
            get { return dic[index]; }
            set { dic[index] = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p["1"] = "";
            p["2"] = "";
            p["3"] = "";
            Console.WriteLine(p["1"]);
            Console.WriteLine(p["2"]);
            Console.WriteLine(p["3"]);
             //输出内容:  索引器
            Console.ReadKey();
        }
    }    

 

posted @ 2021-02-13 17:09  默默依然  阅读(79)  评论(0编辑  收藏  举报