爱玩科技网
您的当前位置:首页C#索引器

C#索引器

来源:爱玩科技网
   class Program
    {
        static void Main(string[] args)
        {
            Sentence s = new Sentence();
            Console.WriteLine(s[3]);
            s[3] = "kangaroo";
            Console.WriteLine(s[3]);
        }
    }


    class Sentence {
        string[] words = "The quick brown fox".Split();
        public string this [int wordNum] {
            get { return words[wordNum]; }
            set { words[wordNum] = value; }
        }
    }

索引器会在内部编译为名为get_Item和set_Item方法:

    public string get_Item(int wordNum) { }
    public void set_Item(int wordNum, string value) { }

因篇幅问题不能全部显示,请点此查看更多更全内容