C# 索引器实现方法 C#如何让类支持中括号索引

来源:这里教程网 时间:2026-02-21 17:40:04 作者:

索引器的基本写法:用
this[]
定义访问逻辑

类要支持中括号索引,必须声明一个名为

this
的索引器成员,它本质是带参数的属性。语法上类似属性,但参数列表写在方括号里,且必须有
get
set
(或两者):

public string this[int index]
{
    get
    {
        if (index = _items.Length) throw new IndexOutOfRangeException();
        return _items[index];
    }
    set
    {
        if (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<t>[int i]</t>
是非法语法)。如果需要类型灵活性,得把整个类泛型化:

public class SafeList<T>
{
    private readonly List<T> _inner = new();
<pre class="brush:php;toolbar:false;">public T this[int index]
{
    get => _inner[index];
    set => _inner[index] = value;
}

}

此时

T
来自类定义,索引器复用它。若硬要在非泛型类里支持多种返回类型,只能靠
object
dynamic
,但会丢失类型安全和性能。

实际用索引器时最容易忽略的是异常语义——.NET 基类库约定越界抛

IndexOutOfRangeException
,而非
ArgumentException
;另外,索引器不参与序列化,默认不会被 JSON.NET 或 System.Text.Json 输出,需额外标注或手动处理。

相关推荐