Search

yield return to also yield collections by Kirill Osenkov - MSFT

Closed
as Won't Fix Help for as Won't Fix

75
0
Sign in
to vote
Type: Suggestion
ID: 256934
Opened: 2/7/2007 12:54:58 PM
Access Restriction: Public
0
Workaround(s)
Suppose we have:

public IEnumerable<string> GetStrings()
{
yield return "first";
yield return "last";
}

and now we'd like to write:

public IEnumerable<string> GetStrings(IEnumerable<string> innerList)
{
yield return "first";
yield return innerList;
yield return "last";
}

instead of:

public IEnumerable<string> GetStrings(IEnumerable<string> innerList)
{
yield return "first";
foreach (string s in innerList)
{
    yield return s;
}
yield return "last";
}

which is, basically, enabling yield return to also return (and flatten) anything which implements IEnumerable of the same type.

I guess it should be pretty easy for the compiler to spit out foreach and checking the types - it would be nice to have such a flattening feature like in Comega.

Thanks!
Details (expand)
Product Language
English

Version

Visual Studio 2005 (All Products and Editions)
Operating System
Windows XP Professional
Operating System Language
English
Problem Statement
Currently yield return in a member of type IEnumerable<T> cannot return things which implement IEnumerable<T>.
Proposed Solution
Make the compiler to wrap yield return List<T> in a foreach statement which yield returns every member.
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey.

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by exoteric on 8/10/2010 at 1:20 PM
It's more than two years since this item was closed and Visual Studio 2010 with .NET 4.0 has since shipped. Have you reconsidered this for vNext? It's a great feature and it's sad to see the poor Linq performance resulting from the absence of this feature.
Posted by Mike Chaliy on 3/2/2010 at 11:16 AM
F# already have this guy and this is very useful.
Posted by Microsoft on 4/24/2008 at 4:38 PM
Thanks again for your suggestion. After having done feature planning for the next release of C# I regret to say that this feature is not being added. We have to do some harsh prioritization, both because of our implementation and testing resources, but also because we need to keep the number of new langauge features at a manageable level - depending on how you count, we are adding only four language features to C# this time around. Unfortunately many great suggestions just can't make it in because of that.

I apologize that this is a "canned" follow-up answer, sent out as a result of our feature planning for the next release. In most cases I or someone else already replied individually to your suggestion - please let us know if you feel it hasn't been adequately addressed.

Thanks again for taking the time to share your ideas with us. Please keep them coming!

Mads Torgersen, C# Language PM.
Posted by Microsoft on 5/1/2007 at 6:07 PM
Thank you for your proposal.

We are actively considering the "yield foreach" construct that Wes is describing in his blog. It has not only usability but also performance benefits.

Thanks again,

Mads Torgersen, C# Language PM
Posted by Kirill Osenkov - MSFT on 4/5/2007 at 4:11 PM
Wes Dyer has a great blog post about this:

http://blogs.msdn.com/wesdyer/archive/2007/03/23/all-about-iterators.aspx

I guess "yield foreach" is the best choice, because no new keyword is introduced (foreach is already a keyword).
Sign in to post a workaround.