yield return to also yield collections - by Kirill Osenkov - MSFT

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


76
0
Sign in
to vote
ID 256934 Comments
Status Closed Workarounds
Type Suggestion Repros 0
Opened 2/7/2007 12:54:58 PM
Access Restriction Public

Description

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!
Sign in to post a comment.
Posted by BigbearZhu on 1/7/2014 at 2:52 PM
Please reconsider this feature or at least re-enable the voting! We need to see how many people need it and it is so useful for data-intensive applications!
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).