要声明类或结构上的索引器,要使用 this 关键字,如下例所示:
public int this[int index] // Indexer declaration
{
// get and set accessors
}
{
// get and set accessors
}
Demo 1
下面的Demo说明如何声明私有数组字段、arr 和索引器。使用索引器可直接访问实例 test[i]。另一种使用索引器的方法是将数组声明为 public 成员并直接访问它的成员 arr[i]。
using System;
using System.Collections.Generic;
using System.Text;
namespace JackyGao
{
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 Program
{
static void Main(string[] args)
{
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]);
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace JackyGao
{
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 Program
{
static void Main(string[] args)
{
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 #0 = 0
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
PS:当计算索引器的访问时(例如,在 Console.Write 语句中),将调用 get 访问器。因此,如果 get 访问器不存在,将发生编译时错误。
使用其他值进行索引
C# 并不将索引类型限制为整数。例如,对索引器使用字符串可能是有用的。通过搜索集合内的字符串并返回相应的值,可以实现此类的索引器。由于访问器可被重载,字符串和整数版本可以共存。
Demo 2
在此例中,声明了存储星期几的类。声明了一个 get 访问器,它接受字符串(天名称),并返回相应的整数。例如,星期日将返回 0,星期一将返回 1,等等。
using System;
using System.Collections.Generic;
using System.Text;
namespace JackyGao
{
// 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"]);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace JackyGao
{
// 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"]);
}
}
}
输出:
5
-1
提高索引器的安全性和可靠性有两种主要的方法:
1、当设置并检索来自索引器访问的任何缓冲区或数组的值时,请始终确保您的代码执行范围和类型检查。
2、应当为 get 和 set 访问器的可访问性设置尽可能多的限制。这一点对 set 访问器尤为重要。