你能描述一下它的功能吗?我在其中一个项目中遇到过它,但不知道它是如何工作的。类中的奇怪属性
public object this[int i]
{
get { return columnValues[i]; }
}
你能描述一下它的功能吗?我在其中一个项目中遇到过它,但不知道它是如何工作的。类中的奇怪属性
public object this[int i]
{
get { return columnValues[i]; }
}
这就是所谓的索引,用于索引,比如我们用它来从字符串获得字符。你可以准备一下吧here,或here,
string str = "heel";
char chr = str[0];
这是如何索引可以为类
class Sentence
{
string[] words = "The quick brown fox".Split();
public string this [int wordNum] // indexer
{
get { return words [wordNum]; }
set { words [wordNum] = value; }
}
}
Sentence s = new Sentence();
Console.WriteLine (s[3]); // fox
s[3] = "kangaroo";
Console.WriteLine (s[3]); // kangaroo
[已编入索引的属性](http://msdn.microsoft.com/zh-cn/library/aa288464(v = vs.71).aspx)。 –
另请参阅[索引器](http://msdn.microsoft.com/zh-cn/library/vstudio/6x16t2tx.aspx)。 – Jon