且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 C# 中重载方括号运算符?

更新时间:2023-02-18 15:27:45

你可以找到如何去做 此处.简而言之就是:

you can find how to do it here. In short it is:

public object this[int i]
{
    get { return InnerList[i]; }
    set { InnerList[i] = value; }
}

如果您只需要一个 getter,也可以使用答案中的语法(从 C# 6 开始).

If you only need a getter the syntax in answer below can be used as well (starting from C# 6).