<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nesting C#'s yield </title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx</link><description>C#'s yield keyword is sure neat, but I notice it only lets you yield a single item. It does not let you yield another enumerator and then flatten for you. You can manually do this by having the for-each yourself. foreach(int i in GetValues()) { yield</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Nesting C#'s yield </title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx#450876</link><pubDate>Fri, 12 Aug 2005 18:19:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450876</guid><dc:creator>ben</dc:creator><description>could you explain what's going on in your &amp;quot;pseudo tail-recursion&amp;quot; code? i having trouble following the logic.&lt;br&gt;&lt;br&gt;thanks.</description></item><item><title>re: Nesting C#'s yield </title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx#450884</link><pubDate>Fri, 12 Aug 2005 18:58:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450884</guid><dc:creator>jmstall</dc:creator><description>Ben - I've updated it with more explanation.</description></item><item><title>re: Nesting C#'s yield </title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx#450939</link><pubDate>Fri, 12 Aug 2005 20:29:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450939</guid><dc:creator>ROn</dc:creator><description>typically how can yield be applied in the real world?  For example I don't often need to calculate the fibonacci sequence.&lt;br&gt;&lt;br&gt;It seems attractive to me for parsing.  Anything else out there?</description></item><item><title>re: Nesting C#'s yield </title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx#450950</link><pubDate>Fri, 12 Aug 2005 20:58:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450950</guid><dc:creator>jmstall</dc:creator><description>Yield is awesome for actually *writing* enumerators. For example, how would you write an enumerator that traverses a tree? &lt;br&gt;Think of it as the other half of for-each.&lt;br&gt;Some examples off yield:&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/brada/archive/2004/3/4.aspx"&gt;http://blogs.msdn.com/brada/archive/2004/3/4.aspx&lt;/a&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/jmstall/archive/2005/08/08/textreader_yield.aspx"&gt;http://blogs.msdn.com/jmstall/archive/2005/08/08/textreader_yield.aspx&lt;/a&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/toub/archive/2004/10/29.aspx"&gt;http://blogs.msdn.com/toub/archive/2004/10/29.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Nesting C#'s yield </title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx#450955</link><pubDate>Fri, 12 Aug 2005 21:10:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450955</guid><dc:creator>ben</dc:creator><description>ok, so the &amp;quot;yield return x&amp;quot; is never executed?</description></item><item><title>explaining in more detail</title><link>http://blogs.msdn.com/jmstall/archive/2005/08/12/nesting-yield.aspx#450990</link><pubDate>Fri, 12 Aug 2005 22:50:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450990</guid><dc:creator>jmstall</dc:creator><description>&lt;br&gt;The &amp;quot;yield return x&amp;quot; is executed, but all it's doing is just propogating the results from GetValues(top-1) back to GetValue(top)&lt;br&gt;            foreach (int x in GetValues(top-1))&lt;br&gt;            {&lt;br&gt;                yield return x;&lt;br&gt;            }&lt;br&gt;&lt;br&gt;&lt;br&gt;Conceptually, it's like:&lt;br&gt;            yield GetValues(top-1)); // this is not actually legal in C#2.0&lt;br&gt;&lt;br&gt;Or think of it this way: in order for GetValues(5) to yield the set {5,4,3,2,1}, it has to call &amp;quot;yield 5&amp;quot;,&amp;quot;yield 4&amp;quot; ... &amp;quot;yield 1&amp;quot;.&lt;br&gt;So in this code:&lt;br&gt;        static IEnumerable&amp;lt;int&amp;gt; GetValues(int top) // &amp;lt;-- assuing top=5&lt;br&gt;        {&lt;br&gt;            if (top == 0) yield break; // done&lt;br&gt;            yield return top;   &amp;lt;--- this will &amp;quot;yield 5&amp;quot;&lt;br&gt;&lt;br&gt;            foreach (int x in GetValues(top-1)) &lt;br&gt;            {&lt;br&gt;                yield return x;  &amp;lt;--- this will loop around and call &amp;quot;yield 4&amp;quot;, &amp;quot;yield 3&amp;quot; ... &amp;quot;yield 1&amp;quot;.&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;&lt;br&gt;You can always play with it under a debugger to get a real feeling for it.</description></item></channel></rss>