C
C#2mo ago
cypherpotato

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
canton72mo 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
333fred2mo ago
Specifically, that's it's not linear, it's quadratic
MarkPflug
MarkPflug2mo 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
Ꜳåąɐȁặⱥᴀᴬ
you would need a sort of SelectMany

Did you find this page helpful?