索引器
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
在下面的示例中,定义了一个泛型类,并为其提供了简单的get和set访问器方法(作为分配和检索值的方法)。Program类为存储字符串创建了此类的一个实例。
private T[] arr = new T[100];
public T this[int i]{
get{
return arr[i];
}
set{
arr[i] = value;
}
}
}
// This class shows how client code uses the indexer
class Program {
static void Main(string[] args){
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
}
}
索引器概述
l 索引器使得对象按照与数组相似的方法进行索引
l get访问器返回值,set访问器分配值
l this关键字用于定义索引器
l value关键字用于定义由set索引器分配的值
l 索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制
l 索引器可被重载
l 索引器可以有多个形参,例如当访问二维数组时
使用索引器
索引器允许您按照处理数组的方式索引类、结构或接口。
要声明类或结构上的索引器,请使用this关键字,如下例所示:
public int this[int index] // Indexer declaration
{
// get and set accessors
}
备注
索引器类型及其参数类型必须至少如同索引器本身一样是可访问的。
索引器的签名由其形参的数量和类型组成。它不包括索引器类型或形参名。如果在同一类中声明一个以上的索引器,则它们必须具有不同的签名。
索引器值不归类为变量,因此,不能将索引器值作为ref或out参数来传递。
要为索引器提供一个其他语言可以使用的名字,请使用声明中的name属性。例如:
[System.Runtime.CompilerServices.IndexerName("TheItem")]
public int this [int index] // Indexer declaration
{
}
此索引器将具有名称TheItem。不提供名称属性将生成Item默认名称。
示例1
下面的示例说明如何声明私有数组字段、arr和索引器。使用索引器可直接访问实例test[i]。另一种使用索引器的方法是将数组声明为public成员并直接访问它的成员arr[i]。
private int[] arr = new int[100];
public int this[int index]{ //Indexer declaration
get{
//Check the index limits.
if (index < 0 || index >= 100){
return 0;
}
else{
return arr[index];
}
}
set{
if (!(index < 0 || index >= 100)){
arr[index] = value;
}
}
}
}
class MainClass {
static void Main(){
IndexerClass test = new IndexerClass();
//Call the indexer to initialize the elements #3 and #5.
test[3] = 256;
test[5] = 1025;
for (int i = 0; i <= 10; i++){
System.Console.WriteLine("Element #{0} = {1}",i,test[i]);
}
}
}
请注意,当计算索引器的访问时(例如,在Console.Write语句中),将调用get访问器。因此,如果get访问器不存在,将发生编译时错误。
使用其他值进行索引
C#并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类的索引器。由于访问器可被重载,字符串和整数版本可以共存。
示例2
在此例中,声明了存储星期几的类。声明了一个get访问器,它接受字符串(天名称),并返回相应的整数。例如,星期日将返回0,星期一将返回1,等等。
class DayCollection {
string[] days = {"Sun","Mon","Tues","Wed","Thurs","Fri","Sat"};
//This method finds the day or returns -1
private int GetDay(string testDay){
int i = 0;
foreach (string day in days){
if (day == testDay){
return i;
}
i++;
}
return -1;
}
//The get accessor returns an integer for a given string
public int this[string day]{
get{
return (GetDay(day));
}
}
}
class Program {
static void Main(string[] args){
DayCollection week = new DayCollection();
System.Console.WriteLine(week["Fri"]);
System.Console.WriteLine(week["Made-up Day"]);
}
}
可靠编程
提高索引器的安全性和可靠型有两种主要的方法:
l 当设置并检索来自索引器访问的任何缓冲区或数组的值时,请始终确保代码执行范围和类型检查
l 应当为get和set访问器的可访问性设置尽可能多的限制。这一点对set访问器尤为重要
接口中的索引器
索引器可在接口上声明。接口索引器的访问器与类索引器的访问器具有以下方面的不同:
l 接口访问器不使用修饰符
l 接口访问器没有体
因此,访问器的用途是指示索引器是读写、只读还是只写。
以下是接口索引器访问器的示例:
public interface ISomeInterface
{
//...
// Indexer declaration:
string this[int index]
{
get;
set;
}
}
一个索引器的签名必须区别于在同一接口中声明的其他所有索引器的签名。
示例
下面的示例显示如何实现接口的索引器。public interface ISomeInterface {
//Indexer declaration
int this[int index]{
get;
set;
}
}
// Implementing the interface.
class IndexerClass : ISomeInterface {
private int[] arr = new int[100];
public int this[int index]{ //indexer declaration
get{
//Check the index limits.
if (index < 0 || index >= 100){
return 0;
}
else{
return arr[index];
}
}
set{
if (!(index < 0 || index >= 100)){
arr[index] = value;
}
}
}
}
class MainClass2 {
static void Main(){
IndexerClass test = new IndexerClass();
//Call the indexer to initialize the elements #2 and #5.
test[2] = 4;
test[5] = 32;
for (int i = 0; i <= 10; i++){
System.Console.WriteLine("Element #{0} = {1}",i,test[i]);
}
}
}
在上例中,可以通过使用接口成员的完全限定名来使用显式接口成员实现。例如:
public string ISomeInterface.this
{
}
但是,只有当类使用同一索引器签名实现一个以上的接口时,为避免多义性才需要使用完全限定名。例如,如果Employee类实现的是两个接口ICitizen和IEmployee,并且这两个接口具有相同的索引器签名,则必须使用显式接口成员实现。即,以下索引器声明:
public string IEmployee.this
{
}
在IEmployee接口上实现索引器,而以下声明:
public string ICitizen.this
{
}
在ICitizen接口上实现索引器。
属性和索引器之间的比较
索引器和属性类似。除下表中显示的差别外,为属性访问器定义的所有规则同样适用于索引器访问器。
属性 | 索引器 |
允许调用方法,如同它们是公共数据成员。 | 允许调用对象上的方法,如同对象是一个数组 |
可通过简单的名称进行访问。 | 可通过索引器进行访问。 |
可以为静态成员或实例成员。 | 必须为实例成员。 |
属性的get访问器没有参数。 | 索引器的get访问器具有与索引器相同的形参表 |
属性的set访问器包含隐式value参数。 | 除了value参数外,索引器的set访问器还具有索引器相同的形参表。 |