0

Behold the C# code:

 IEnumerable<int> innerMethod(int parameter)
 {
     foreach(var i in Enumerable.Range(0, parameter))
     {
         yield return i;
     }
 }

 IEnumerable<int> outerMethod(int parameter)
 {
     foreach(var i in Enumerable.Range(1, parameter))
     {
         foreach(var j in innerMethod(i))
         {
              yield return j;
         }
     }
 }

The question is: There is a way for outerMethod have the same output without iterating over innerMethod output?

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • This is a fairly frequently requested feature usually called "yield foreach". We do not support it, but some "research" variants of C# like C-Omega do. You should read Wes's article about this: http://blogs.msdn.com/wesdyer/archive/2007/03/23/all-about-iterators.aspx – Eric Lippert Jul 22 '09 at 16:19
  • possible duplicate of [Yield Return Many?](http://stackoverflow.com/questions/3851997/yield-return-many) – nawfal Jul 08 '14 at 12:41

1 Answers1

4

Unfortunately not.

In F# you could do something like

yield! innerMethod(i)

but there's no equivalent in C#.

I mean, in this particular case you could replace the method with:

 IEnumerable<int> outerMethod(int parameter)
 {
     return Enumerable.Range(1, parameter)
                      .SelectMany(x => innerMethod(x));
 }

but I expect you wanted a more general purpose way of doing it. (If that helps though, great!)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194