2

Consider the following code:

IEnumerable<string> FooBar()
{
    yield return "foo";
    yield return "bar";
}

IEnumerable<string> FooBarBaz()
{
    foreach(var s in FooBar()) { yield return s; }
    yield return "baz";
}

Is there any way I can refactor FooBarBaz to directly yield everything from FooBar(), without having to explicitly type out the foreach loop?

I'd like to do something like the following, but this is of course not valid C#:

IEnumerable<string> FooBarBaz()
{
    yield return all FooBar();
    yield return "baz";
}
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402

0 Answers0