Iterator blocks and inheritance

前端 未结 4 1983
心在旅途
心在旅途 2021-02-13 01:58

Given a base class with the following interface:

public class Base
{
    public virtual IEnumerable GetListOfStuff()
    {
        yield return \"F         


        
4条回答
  •  有刺的猬
    2021-02-13 02:27

    It seems that one solution was to simply follow what the "manual" says: make a helper function.

    So for now I've solved it like this:

    public class Derived : Base
    {
        private IEnumerable GetBaseStuff()
        {
            return base.GetListOfStuff();
        }
    
        public override IEnumerable GetListOfStuff()
        {
            foreach (string s in GetBaseStuff())
            {
                yield return s;
            }
    
            yield return "Fourth";
            yield return "Fifth";
        }
    }
    

    But I'm curious about other solutions as well, should they exist.

提交回复
热议问题