现在的位置: 首页 > 综合 > 正文

.net盲点:索引器Indexers (转)

2012年05月28日 ⁄ 综合 ⁄ 共 2648字 ⁄ 字号 评论关闭

索引器Indexers

C#引入了一个索引器(Indexers)的功能,它能让我们像处理数组一样处理对象。在C#社区中,我们又把它叫做“智能数组(smart arrays)”。定义C#索引器就像定义属性一样方便。(这里“属性”指property,.

下面是它的结构

<modifier> <return type> this [argument list]
...{
get
...{
// Get codes goes here
 }
set
...{
// Set codes goes here
 }
 }

注:

modifier:修饰词,如private, public, protected or internal

this:在C#中this是一个特殊的关键字,它表示引用类的当前实例。在这里它的意思是当前类的索引。

argument list:这里指索引器的参数。

它有几个规则:1. ref 和 out 参数不能使用

              2. 至少有一个参数

              3. 尽量使用整数或者字符串类型的参数

下面是一个索引器的例子

using System;
using System.Collections;

class MyClass
...{
private string []data = new string[5];
public string this [int index]
...{
get
...{
return data[index];
        }
set
...{
        data[index] = value;
       }
   }
}


class MyClient
...{
public static void Main()
...{
      MyClass mc = new MyClass();
      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//基类有一个索引器
...{
public int this[int indxer]
...{
get
...{
   Console.Write("Base GET");
return 10;
 }
set
...{
   Console.Write("Base SET");
 }
 }
}

class Derived : Base
...{

}
class MyClient
...{
public static void Main()
...{
 Derived d1 = new Derived();
 d1[0] = 10;
 Console.WriteLine(d1[0]);//照样使用索引器
 }
}
//输出
Base SET
Base GET
10

索引器(Indexers)和多态

using System;

class Base
...{
public virtual int this[int index]//有一个虚的索引器
...{
get
...{
   Console.Write("Base GET");
return 10;
 }
set
...{
   Console.Write("Base SET");
 }
 }
}

class Derived : Base
...{
public override int this[int index]
...{
get
...{
   Console.Write("Derived GET");
return 10;
 }
set
...{
   Console.Write("Derived SET");
 }
 }
}

class MyClient
...{
public static void Main()
...{
 Base b1 = new Derived();
 b1[0]= 10;
 Console.WriteLine(b1[0]);//这个时候也是显示Derived对象的索引器
  }
}
//输出
Derived SET
Derived GET
10

抽象的索引器(Indexers)

using System;

abstract class Abstract
...{
public abstract int this[int index]
...{
get;
set;
 }
}

class Concrete : Abstract
...{
public override int this[int index]
...{
get
...{
   Console.Write(" GET");
return 10;
 }
set
...{
   Console.Write(" SET");
 }
 }
}

class MyClient
...{
public static void Main()
...{
 Concrete c1 = new Concrete();
 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)的索引器。

抱歉!评论已关闭.