C#: yield return range/collection

后端 未结 3 1259
夕颜
夕颜 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:35

    The yield keyword is indeed very nice. But nesting it in a for loop will cause more glue code to be generated and executed.

    If you can live with a less functional style of programming, you can pass a List around to which you append:

     void GenerateList(List result)
     {
    
          result.Add("something")
    
          // more code.
    
          GenerateList(result);
    
     }
    

提交回复
热议问题