事件为类和类的实例提供了向外界发送通知的能力,而索引指示器则可以像数组那样对对象进行索引访问。
事件所声明的类型必须是一个代表类型,代表类型应预先声明。
索引指示器例子:
using System; namespace Welcome { class Program { static void Main(string[] args) { Index index = new Index(); Console.WriteLine(index[2]); index[2] = 0; for (int i = 0; i < 5; i++) { Console.WriteLine(index[i]); } Console.Read(); } } class Index { private int[] temp = {1,2,3,4,5 }; /** * 索引指示器,主要效果是封装。 */ public int this[int index] { get { return temp[index]; } set{ temp[index] = value; } } } }
结果: