Can I unpack some results from another IEnumerable into my iterator function?

Eg.:
IEnumerable<T> Foo() {
yield return new T();
yield return FunctionWhichBringsMoreTs<T>(); // which is IEnumerable<T> too
}
IEnumerable<T> Foo() {
yield return new T();
yield return FunctionWhichBringsMoreTs<T>(); // which is IEnumerable<T> too
}
or I have to foreach the other function too?
4 Replies
canton7
canton74d ago
No, you need an inner loop There have been requests for some syntax there (like python's yield from), but it's been rejected on the grounds that it hides the extra cost of iterating the child collection
333fred
333fred4d ago
Specifically, that's it's not linear, it's quadratic
MarkPflug
MarkPflug4d ago
I remember this originally as a proposed "yield foreach" keyword, but it would require incompatible library changes that made it impractical. Blog post from almost 2 decades ago (damn, I'm getting old): https://learn.microsoft.com/en-us/archive/blogs/wesdyer/all-about-iterators
this_is_pain
this_is_pain3d ago
you would need a sort of SelectMany

Did you find this page helpful?