1、C#中提供了按照索引器进行访问的方法。
2、定义索引器的方式:string this[int index]{get { return ""; }set { }},string为索引器的类型,[]中是参数列表。进行索引器写操作就是调用set代码块,在set内部使用value得到用户设置的值;进行读操作就执行get代码块。
3、索引器参数可以不止一个,类型也不限于int,几乎可以是任意类型。this[int index],this[string s], 。索引器的名字必须是this。
4、索引器是可以重载的。
5、程序员说要有属性,所以就有了属性。索引器同理。
6、索引器的本质,反编译之。
7、之前用到索引器的地方:string类char c = s1[2]。
8、索引器也可以只读,只要没有set段就可以了。
9、索引器。在c语言中叫下标,在C#中叫索引。
string s=”abc”;
char ch=s[0]; //其实这也是一种索引
10、例1:
public string this[string s, bool b]
{
get
{
return “”;
}
}
11、例2:
public string this[int index]
{
get
{
}
set
{
}
}