C# 索引器完全指南:让对象像数组一样访问、重载与多参数索引器
索引器允许对象像数组一样被索引。为类定义索引器后,该类表现如虚拟数组。可使用数组访问操作符 [ ] 访问类实例。
语法
一维索引器语法:
element-type this[int index] {
get { /* 返回 index 指定的值 */ }
set { /* 设置 index 指定的值 */ }
}
索引器用法
声明类似属性,使用 get/set 访问器。但属性返回/设置特定数据成员,索引器返回/设置对象实例的特定值——将实例数据分割成更小部分并索引。
属性有名称,索引器无名称,用 this 关键字指代对象实例。
基本示例
using System;
namespace IndexerApplication {
class IndexedNames {
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames() {
for (int i = 0; i < size; i++) namelist[i] = "N. A.";
}
public string this[int index] {
get {
string tmp;
if (index >= 0 && index <= size - 1) tmp = namelist[index];
else tmp = "";
return tmp;
}
set {
if (index >= 0 && index <= size - 1) namelist[index] = value;
}
}
static void Main(string[] args) {
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
names[4] = "Davinder";
names[5] = "Sunil";
names[6] = "Rubic";
for (int i = 0; i < IndexedNames.size; i++) Console.WriteLine(names[i]);
Console.ReadKey();
}
}
}
输出:
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
重载索引器
索引器可重载。可声明多参数,每参数类型可不同。索引不限于整数,如字符串。
多参数/字符串索引器示例
using System;
namespace IndexerApplication {
class IndexedNames {
private string[] namelist = new string[size];
static public int size = 10;
public IndexedNames() {
for (int i = 0; i < size; i++) namelist[i] = "N. A.";
}
// 整数索引器
public string this[int index] {
get {
if (index >= 0 && index < size) return namelist[index];
return "";
}
set {
if (index >= 0 && index < size) namelist[index] = value;
}
}
// 字符串索引器:按名字查找索引
public int this[string name] {
get {
for (int i = 0; i < size; i++)
if (namelist[i] == name) return i;
return -1; // 未找到
}
}
static void Main(string[] args) {
IndexedNames names = new IndexedNames();
names[0] = "Zara"; names[1] = "Riz"; names[2] = "Nuha";
names[3] = "Asif"; names[4] = "Davinder"; names[5] = "Sunil"; names[6] = "Rubic";
// 使用整数索引器
for (int i = 0; i < IndexedNames.size; i++) Console.WriteLine(names[i]);
// 使用字符串索引器
Console.WriteLine(names["Nuha"]); // 2
Console.ReadKey();
}
}
}
输出:
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
多维索引器
支持多个参数,模拟多维数组:
public double this[int row, int col] {
get { return matrix[row, col]; }
set { matrix[row, col] = value; }
}
// 使用
Matrix m = new Matrix(3, 3);
m[1, 2] = 3.14;
double val = m[1, 2];
索引器 vs 属性
| 特性 | 属性 | 索引器 |
|---|---|---|
| 标识 | 名称 | this 关键字 |
| 参数 | 无 | 至少一个(索引) |
| 访问方式 | obj.Prop | obj[index] |
| 重载 | 不可重载 | 可重载(不同参数列表) |
| 静态 | 可为 static | 不能为 static |
| 用途 | 单值特征 | 集合/容器元素访问 |
接口中声明索引器
public interface IStringList {
string this[int index] { get; set; }
int Count { get; }
}
public class StringList : IStringList {
private List list = new List();
public string this[int index] {
get => list[index];
set => list[index] = value;
}
public int Count => list.Count;
}
最佳实践
- 索引器参数不宜过多(1-2 个最佳)
- 索引越界抛出
IndexOutOfRangeException或返回默认值,保持一致性 - 实现
IEnumerable<T>配合foreach使用 - 考虑实现
ICollection/IList获得完整集合语义 - 字符串索引器适合查找场景,但注意性能(O(n) 线性查找)
C语言