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) { }