新运算符-';新';方法

本文关键字:方法 运算符 | 更新日期: 2023-09-27 18:00:00

我的类MyClass:中有这段代码

public new MyClass this[int index]
    {
        get
        {
            if (Count > index)
            {
                return this[index];
            }
            //...MyActions...
            return null;
        }
    }

字符串中。。。

return this[index]

我有递归,但我需要使用base类中的属性。我不知道怎么做。

示例:

 return base.this[index]

但我不能"重写"这个方法,只能设置"new"。我很难过

怎么做到的?对不起,我的英语很差,谢谢

新运算符-';新';方法

您可以使用base关键字访问基类的成员,包括索引器。尝试使用下一个代码片段来调用基类上的索引器:

return base[index];

然后根据需要使用base

return base[index];

例如:

public class A {
  public object this[int index] { get; }
}
public class B : A {
  public object this[int index] {
    get { return base[index]; }
  }
}