.net程序员的盲点(四):索引器Indexers

索引器Indexers
C# 引入了一个索引器(Indexers)的功能,它能让我们像处理数组一样处理对象。在C#社区中,我们又把它叫做“智能数组(smart arrays)”。定义C#索引器就像定义属性一样方便。(这里“属性”指property, .net程序员的盲点(二):两个“属性”引起的歧异-property和attribute的区别
下面是它的结构
< modifier > < return type > this [argumentlist]
... {
get
...{
//Getcodesgoeshere
}

set
...{
//Setcodesgoeshere
}

}

注:
modifier :修饰词,如private, public, protected or internal
this :在C#中this是一个特殊的关键字,它表示 引用类的当前实例。在这里它的意思是当前类的索引。
argument list :这里指索引器的参数。
它有几个规则:1. ref 和 out 参数不能使用
2. 至少有一个参数
3. 尽量使用 整数或者字符串类型的参数
下面是一个索引器的例子
using System;
using System.Collections;

class MyClass
... {
privatestring[]data=newstring[5];
publicstringthis[intindex]
...{
get
...{
returndata[index];
}

set
...{
data[index]
=value;
}

}

}



class MyClient
... {
publicstaticvoidMain()
...{
MyClassmc
=newMyClass();
mc[
0]="Rajesh";//mc是一个对象,但是却像个数组,不是吗:)
mc[1]="A3-126";
mc[
2]="Snehadara";
mc[
3]="Irla";
mc[
4]="Mumbai";
Console.WriteLine(
"{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}

}

下面分别举几个继承、多态和抽象类与索引器的例子
索引器(Indexers)和继承
using System;
class Base // 基类有一个索引器
... {
publicintthis[intindxer]
...{
get
...{
Console.Write(
"BaseGET");
return10;
}

set
...{
Console.Write(
"BaseSET");
}

}

}


class Derived:Base
... {

}

class MyClient
... {
publicstaticvoidMain()
...{
Derivedd1
=newDerived();
d1[
0]=10;
Console.WriteLine(d1[
0]);//照样使用索引器
}

}

// 输出
BaseSET
BaseGET
10
索引器(Indexers)和多态
using System;

class Base
... {
publicvirtualintthis[intindex]//有一个虚的索引器
...{
get
...{
Console.Write(
"BaseGET");
return10;
}

set
...{
Console.Write(
"BaseSET");
}

}

}


class Derived:Base
... {
publicoverrideintthis[intindex]
...{
get
...{
Console.Write(
"DerivedGET");
return10;
}

set
...{
Console.Write(
"DerivedSET");
}

}

}


class MyClient
... {
publicstaticvoidMain()
...{
Baseb1
=newDerived();
b1[
0]=10;
Console.WriteLine(b1[
0]);//这个时候也是显示Derived对象的索引器
}

}

// 输出
DerivedSET
DerivedGET
10
抽象的索引器(Indexers)
using System;

abstract class Abstract
... {
publicabstractintthis[intindex]
...{
get;
set;
}

}


class Concrete:Abstract
... {
publicoverrideintthis[intindex]
...{
get
...{
Console.Write(
"GET");
return10;
}

set
...{
Console.Write(
"SET");
}

}

}


class MyClient
... {
publicstaticvoidMain()
...{
Concretec1
=newConcrete();
c1[
0]=10;
Console.WriteLine(c1[
0]);
}

}

// 输出
SET
GET
10
索引器和属性(Property)
1. 索引器是以它的标识符来作为参数标识的。但是属性是以它的名称来作为参数标识的。
注:this [int i1]是以int作为它的标识,和this [int i2]属于同一个索引器。
2. 索引器一般访问的实例对象。但是属性一般访问的是静态对象。
3. 索引器一般使用数组的方式来访问。但是属性一般使用对象方法的方式访问。
注:索引器
MyClass mc = new MyClass();
mc[0] = "Rajesh";
属性
MyClass mc = new MyClass();
mc.Name = "Rajesh";
小结
1. 索引器是可以进行重载、多态和抽象的,这点和一般的成员函数没有什么区别。
2. 参数列表(argument list)可以有几个参数组成。this [int i1,int i2]
3. 一个类存在两个或者两个以上的索引器时,参数列表(argument list)必须不同。this [int i1]和this [int i2]属于同一个索引器;this [int i1]和this [string s2]属于不同的索引器;
4.C# 没有静态(static)的索引器。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值