C#: yield return range/collection

后端 未结 3 1261
夕颜
夕颜 2020-12-16 09:01

I use the yield return keyword quite a bit, but I find it lacking when I want to add a range to the IEnumerable. Here\'s a quick example of what I

3条回答
  •  眼角桃花
    2020-12-16 09:43

    If you already have an IEnumerable to loop over, and the return type is IEnumerable (as is the case for functions that could use yield return), you can simply return that enumeration.

    If you have cases where you need to combine results from multiple IEnumerables, you can use the IEnumerable.Concat extension method.

    In your recursive example, though, you need to terminate the enumeration/concatenation based on the contents of the enumeration. I don't think my method will support this.

提交回复
热议问题