C#学习笔记四-----索引器 (来自微软MSDN)
1 索引器
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于 属性 ,不同之处在于它们的访问器采用参数。
在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索值的方法)。 Program 类为存储字符串创建了此类的一个实例。
C#
class SampleCollection<T>
{
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]);
}
}
索引器概述
· get 访问器返回值。 set 访问器分配值。
· this 关键字用于定义索引器。
· value 关键字用于定义由 set 索引器分配的值。
· 索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
· 索引器可被重载。
· 索引器可以有多个形参,例如当访问二维数组时。
2. 使用索引器( C# 编程指南)
索引器允许您按照处理数组的方式索引 类 、 结构 或 接口 。要声明类或结构上的索引器,请使用 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] 。
C#
class IndexerClass
{
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] = 1024;
for (int i = 0; i <= 10; i++)
{
System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
}
}
}
输出
Element #1 = 0
Element #2 = 0
Element #3 = 256
Element #4 = 0
Element #5 = 1024
Element #6 = 0
Element #7 = 0
Element #8 = 0
Element #9 = 0
Element #10 = 0
请注意,当计算索引器的访问时(例如,在 Console.Write 语句中),将调用 get 访问器。因此,如果 get 访问器不存在,将发生编译时错误。
使用其他值进行索引
C# 并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类的索引器。由于访问器可被重载,字符串和整数版本可以共存。
示例 2
在此例中,声明了存储星期几的类。声明了一个 get 访问器,它接受字符串(天名称),并返回相应的整数。例如,星期日将返回 0 ,星期一将返回 1 ,等等。
C#
// Using a string as an indexer value
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"]);
}
}
输出
-1
可靠编程
· 当设置并检索来自索引器访问的任何缓冲区或数组的值时,请始终确保您的代码执行范围和类型检查。
· 应当为 get 和 set 访问器的可访问性设置尽可能多的限制。这一点对 set 访问器尤为重要。
3 接口中的索引器
索引器可在 接口( C# 参考) 上声明。接口索引器的访问器与 类 索引器的访问器具有以下方面的不同:
· 接口访问器不使用修饰符。
· 接口访问器没有体。
因此,访问器的用途是指示索引器是读写、只读还是只写。
以下是接口索引器访问器的示例:
C#
public interface ISomeInterface
{
//...
// Indexer declaration:
string this[int index]
{
get;
set;
}
}
一个索引器的签名必须区别于在同一接口中声明的其他所有索引器的签名。
示例
C#
// Indexer on an interface:
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 MainClass
{
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]);
}
}
}
输出
Element #0 = 0
Element #1 = 0
Element #2 = 4
Element #3 = 0
Element #4 = 0
Element #5 = 32
Element #6 = 0
Element #7 = 0
Element #8 = 0
Element #9 = 0
Element #10 = 0
在上例中,可以通过使用接口成员的完全限定名来使用显式接口成员实现。例如:
public string ISomeInterface.this
{
}
但是,只有当类使用同一索引器签名实现一个以上的接口时,为避免多义性才需要使用完全限定名。例如,如果 Employee 类实现的是两个接口 ICitizen 和 IEmployee ,并且这两个接口具有相同的索引器签名,则必须使用显式接口成员实现。即,以下索引器声明:
public string IEmployee.this
{
}
在 IEmployee 接口上实现索引器,而以下声明:
public string ICitizen.this
{
}
在 ICitizen 接口上实现索引器。
4 属性和索引器之间的比较 .
索引器与属性类似。除下表中显示的差别外,为属性访问器定义的所有规则同样适用于索引器访问器。
差别:
属性 :允许调用方法,如同它们是公共数据成员;索引器:允许调用对象上的方法,如同对象是一个数组。
属性 :可通过简单的名称进行访问;索引器 :可通过索引器进行访问。
属性 :可以为静态成员或实例成员;索引器:必须为实例成员。
属性 的 get 访问器没有参数;索引器的 get 访问器具有与索引器相同的形参表。
属性 的 set 访问器包含隐式 value 参数。 除了 value 参数外,索引器的 set 访问器还具有与索引器相同的形参表。