给定一个具有以下接口(interface)的基类:
public class Base
{
public virtual IEnumerable<string> GetListOfStuff()
{
yield return "First";
yield return "Second";
yield return "Third";
}
}
我想创建一个重写方法的派生类,并添加自己的东西,如下所示:
public class Derived : Base
{
public override IEnumerable<string> GetListOfStuff()
{
foreach (string s in base.GetListOfStuff())
{
yield return s;
}
yield return "Fourth";
yield return "Fifth";
}
}
但是,我收到警告“无法验证通过迭代器中的基本关键字访问成员”。
那么这个问题的公认解决方案是什么?
最佳答案
怎么样:
public class Derived : Base
{
public override IEnumerable<string> GetListOfStuff()
{
return base.GetListOfStuff().Concat(GetMoreStuff());
}
private IEnumerable<string> GetMoreStuff()
{
yield return "Fourth";
yield return "Fifth";
}
}
关于c# - 迭代器 block 和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432821/