回顾:

概念:


class Person { private Person[] friends; public Person this[int index] { get { return friends[index]; } set { friends[index] = value; } } }
索引器看起来很像一个名叫this的函数,但后面是[],里面就像成员属性.
还记得概念说的可以像数组一样访问吗
Person p = new Person(); p[0]=new Person(); //使用set语句块. Console.WriteLine(p[0]); //使用get语句块
public Person this[int index] { get { return friends[index]; } set { //根据需求可以写里面的逻辑. friends[index] = value; } }
这个不多说.
换句话说:快速操作数组的成员属性;
注意
结构体里面也可以用索引器.
本文作者:Leemoon
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!