Iterator blocks and inheritance

前端 未结 4 1877
無奈伤痛
無奈伤痛 2021-02-13 01:47

Given a base class with the following interface:

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


        
4条回答
  •  悲哀的现实
    2021-02-13 02:39

    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.

提交回复
热议问题