C# 索引器
索引器是一种特殊类型的属性,它允许像数组一样访问类或结构的内部集合。C# 允许我们定义自定义索引器、泛型索引器,以及重载索引器。
索引器的定义方式与属性相同,使用 `this` 关键字和方括号 `[]`。
语法
return type this[parameter-type index] { get { // return the value from the specified index of an internal collection } set { // set values at the specified index in an internal collection } }
以下示例在类中定义了一个索引器。
示例:索引器
class StringDataStore
{
private string[] strArr = new string[10]; // internal data storage
public string this[int index]
{
get
{
if (index < 0 || index >= strArr.Length)
throw new IndexOutOfRangeException("Index out of range");
return strArr[index];
}
set
{
if (index < 0 || index >= strArr.Length)
throw new IndexOutOfRangeException("Index out of range");
strArr[index] = value;
}
}
}
上面的 `StringDataStore` 类为其私有数组 `strArr` 定义了一个索引器。因此,现在您可以像数组一样使用 `StringDataStore` 来从 `strArr` 中添加和检索字符串值,如下所示。
示例:索引器
StringDataStore strStore = new StringDataStore();
strStore[0] = "One";
strStore[1] = "Two";
strStore[2] = "Three";
strStore[3] = "Four";
for(int i = 0; i < 10 ; i++)
Console.WriteLine(strStore[i]);
输出
OneTwo
Three
Four
从 C# 7 开始,您可以对 get 和 set 使用表达式主体语法。
示例:索引器
class StringDataStore
{
private string[] strArr = new string[10]; // internal data storage
public string this[int index]
{
get => strArr[index];
set => strArr[index] = value;
}
}
泛型索引器
索引器也可以是泛型的。以下泛型类包含泛型索引器。
示例:泛型索引器
class DataStore<T>
{
private T[] store;
public DataStore()
{
store = new T[10];
}
public DataStore(int length)
{
store = new T[length];
}
public T this[int index]
{
get
{
if (index < 0 && index >= store.Length)
throw new IndexOutOfRangeException("Index out of range");
return store[index];
}
set
{
if (index < 0 || index >= store.Length)
throw new IndexOutOfRangeException("Index out of range");
store[index] = value;
}
}
public int Length
{
get
{
return store.Length;
}
}
}
上述泛型索引器可与任何数据类型一起使用。以下示例演示了泛型索引器的使用。
示例
DataStore<int> grades = new DataStore<int>();
grades[0] = 100;
grades[1] = 25;
grades[2] = 34;
grades[3] = 42;
grades[4] = 12;
grades[5] = 18;
grades[6] = 2;
grades[7] = 95;
grades[8] = 75;
grades[9] = 53;
for (int i = 0; i < grades.Length;i++)
Console.WriteLine(grades[i]);
DataStore<string> names = new DataStore<string>(5);
names[0] = "Steve";
names[1] = "Bill";
names[2] = "James";
names[3] = "Ram";
names[4] = "Andy";
for (int i = 0; i < names.Length;i++)
Console.WriteLine(names[i]);
重载索引器
您可以为索引重载不同的数据类型。以下示例重载了一个同时带有 int 类型索引和 string 类型索引的索引器。
示例:重载索引器
class StringDataStore
{
private string[] strArr = new string[10]; // internal data storage
// int type indexer
public string this[int index]
{
get
{
if (index < 0 || index >= strArr.Length)
throw new IndexOutOfRangeException("Index out of range");
return strArr[index];
}
set
{
if (index < 0 || index >= strArr.Length)
throw new IndexOutOfRangeException("Index out of range");
strArr[index] = value;
}
}
// string type indexer
public string this[string name]
{
get
{
foreach (string str in strArr){
if(str.ToLower() == name.ToLower())
return str;
}
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
StringDataStore strStore = new StringDataStore();
strStore[0] = "One";
strStore[1] = "Two";
strStore[2] = "Three";
strStore[3] = "Four";
Console.WriteLine(strStore["one"]);
Console.WriteLine(strStore["two"]);
Console.WriteLine(strStore["Three"]);
Console.WriteLine(strStore["Four"]);
}
}
注意
索引器不允许 ref 和 out 参数。