索引器用this[]定义,是带参数的特殊属性,支持int/string/自定义类型参数但不可仅靠返回值重载;多参数需用元组或结构体封装;可单独实现get或set控制读写权限。

索引器的基本写法:用 this[] 定义访问逻辑
类要支持中括号索引,必须声明一个名为 this 的索引器成员,它本质是带参数的属性。语法上类似属性,但参数列表写在方括号里,且必须有 get 或 set(或两者):
public string this[int index]
{
get
{
if (index < 0 || index >= _items.Length) throw new IndexOutOfRangeException();
return _items[index];
}
set
{
if (index < 0 || index >= _items.Length) throw new IndexOutOfRangeException();
_items[index] = value;
}
}注意:this 是关键字,不能省略;参数类型不限于 int,可以是 string、Guid 甚至自定义类型;但不支持重载仅靠返回值区分的多个索引器。
多参数索引器:用元组或自定义结构体传参
C# 不允许直接写 this[a, b] 这种多维语法,但可以通过复合参数实现类似效果。常见做法是用 ValueTuple 或轻量结构体封装坐标:
- 使用命名元组更清晰:
public T this[(int row, int col) position] - 若需频繁调用,建议定义结构体(避免装箱):
public struct GridIndex { public int Row; public int Col; },再声明public T this[GridIndex idx] - 索引器内部仍需自己做越界检查和数据映射,语言不提供自动二维数组语义
只读与只写索引器:控制访问权限
不是所有场景都需要读写能力。比如封装只读集合时,可只提供 get;日志写入器可能只暴露 set:
- 只读示例:
public string this[int i] => i >= 0 && i - 只写示例:
public string this[int i] { set { _buffer.Add(value); } } - 没有
get的索引器无法用于取值表达式(如var x = obj[0];),编译报错CS0154 - 没有
set的则无法赋值(如obj[0] = "x";),报错CS0200
泛型索引器限制:不能直接声明为泛型方法
索引器本身不能带泛型类型参数(如 this 是非法语法)。如果需要类型灵活性,得把整个类泛型化:
public class SafeList{ private readonly List _inner = new(); public T this[int index] { get => _inner[index]; set => _inner[index] = value; }}
此时
T来自类定义,索引器复用它。若硬要在非泛型类里支持多种返回类型,只能靠object或dynamic,但会丢失类型安全和性能。实际用索引器时最容易忽略的是异常语义——.NET 基类库约定越界抛
IndexOutOfRangeException,而非ArgumentException;另外,索引器不参与序列化,默认不会被 JSON.NET 或 System.Text.Json 输出,需额外标注或手动处理。









