<?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>The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx</link><description>A secret state machine.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>C# Yield Keyword &amp;laquo; Vasu Balakrishnan&amp;#8217;s Blog</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#9632749</link><pubDate>Wed, 20 May 2009 22:36:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9632749</guid><dc:creator>C# Yield Keyword &amp;laquo; Vasu Balakrishnan&amp;#8217;s Blog</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://vasubalakrishnan.wordpress.com/2009/05/20/c-yield-keyword/"&gt;http://vasubalakrishnan.wordpress.com/2009/05/20/c-yield-keyword/&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9632749" width="1" height="1"&gt;</description></item><item><title>Haugerns Development Escapades  &amp;raquo; Blog Archive   &amp;raquo; Beware of the IEnumerables</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#9002775</link><pubDate>Fri, 17 Oct 2008 12:00:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9002775</guid><dc:creator>Haugerns Development Escapades  &amp;raquo; Blog Archive   &amp;raquo; Beware of the IEnumerables</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.haugern.net/blog/beware-of-the-ienumerables/"&gt;http://www.haugern.net/blog/beware-of-the-ienumerables/&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9002775" width="1" height="1"&gt;</description></item><item><title>Tricky IEnumerable&lt;T&gt;</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8925072</link><pubDate>Thu, 04 Sep 2008 22:44:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8925072</guid><dc:creator>the blog =&gt; anything goes</dc:creator><description>&lt;p&gt;Implementing IEnumerable&amp;amp;lt;T&amp;amp;gt; can turned out to be tricky in one particular situation. Consider the&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8925072" width="1" height="1"&gt;</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8869244</link><pubDate>Fri, 15 Aug 2008 12:41:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8869244</guid><dc:creator>Andrew</dc:creator><description>&lt;p&gt;@CodeMage&lt;/p&gt;
&lt;p&gt;&amp;gt; ...By creating a value type that implements the iterator design contract...&lt;/p&gt;
&lt;p&gt;Exactly, and that's what bothers me: why didn't they implement 'yield return' to generate structs? Raymond answered above, but it still somehow not quite persuading&lt;/p&gt;
&lt;p&gt;(btw - that nested value type iterator should be public, that's possibly something you cannot always afford)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8869244" width="1" height="1"&gt;</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8869228</link><pubDate>Fri, 15 Aug 2008 12:35:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8869228</guid><dc:creator>Andrew</dc:creator><description>&lt;p&gt;[You focused on boxing and missed the original problem. DoSomething2(e) will not resume where DoSomething1(e) left off. And stack overflow is not the issue here either. -Raymond]&lt;/p&gt;
&lt;p&gt;Wow! Raymond, yep I missed the point - ultimately had to write a small app in VS to get to it, sorry :) &lt;/p&gt;
&lt;p&gt;But. Iterators for List&amp;lt;T&amp;gt; and Dictionary&amp;lt;K,V&amp;gt; are public nested structs. Does this mean that perf gain in foreach loops was more important back then, than the continuation idea (implemented in yield return) ?&lt;/p&gt;
&lt;p&gt;(just in case you or someone else here has an explanation)&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Andrew&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8869228" width="1" height="1"&gt;</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8868473</link><pubDate>Fri, 15 Aug 2008 04:56:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8868473</guid><dc:creator>CodeMage</dc:creator><description>&lt;p&gt;Another interesting fact about iterators is the garbage they generate, by virtue of implementing IEnumerator&amp;lt;T&amp;gt; and IEnumerable&amp;lt;T&amp;gt;. Every time you use an iterator in a foreach loop, you'll instantiate the implementation of these interfaces. In case of yield return, this implementation is hidden from you by the compiler, but it still creates garbage.&lt;/p&gt;
&lt;p&gt;There's a way to avoid that, though. By creating a value type that implements the iterator design contract (GetEnumerator, MoveNext, Reset and Current) without implementing the IEnumerator&amp;lt;T&amp;gt; and IEnumerable&amp;lt;T&amp;gt; interfaces, you can still use it in foreach loops but you don't generate any garbage. The bad news is that you have to implement it by hand: there's no syntactic sugar for this.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8868473" width="1" height="1"&gt;</description></item><item><title>Iterators</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8867986</link><pubDate>Fri, 15 Aug 2008 00:33:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8867986</guid><dc:creator>Alexander Morou</dc:creator><description>&lt;p&gt;As stated before, the primary reason the state machine transitions into a final state is to prevent pre-final states from being repeated, since the logic of them could alter the state of other objects within the iterator's scope (and potentially even cause run-time errors).&lt;/p&gt;
&lt;p&gt;I'm guessing the other reason yield break; transitions into a state, is state reduction, the most optimal state machine is the one with the fewest states. &amp;nbsp;Multiple yield breaks do transition into a single state, don't they? (I haven't actually checked.)&lt;/p&gt;
&lt;p&gt;Recursive iterators merely require a more complex approach, but are entirely possible. &amp;nbsp;Instead of being directly recursive, you need to cast part of the logic into a sub-iterator that would effectively emit the transition series of objects used in the recursive approach, then using the flat form of the hierarchy, yield the members respectively. &amp;nbsp;Alternatively you can flatten your recursive approach into a non-recursive solution and yield the members that way.&lt;/p&gt;
&lt;p&gt;I found iterators interesting because you can essentially do whatever you want with them. &amp;nbsp;My favorite was creating a multi-dimensional array iterator that iterated the indices of an array, yielding the same array instance each time, just with different values. &amp;nbsp;Good for debugging where you send in a nondescript array and its internals are iterated and logged regardless of the array's dimensional structure.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8867986" width="1" height="1"&gt;</description></item><item><title>Today&amp;#8217;s Blogs &amp;laquo; Roman&amp;#8217;s Blog</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8867818</link><pubDate>Thu, 14 Aug 2008 23:11:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8867818</guid><dc:creator>Today&amp;#8217;s Blogs &amp;laquo; Roman&amp;#8217;s Blog</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://rhnatiuk.wordpress.com/2008/08/14/todays-blogs/"&gt;http://rhnatiuk.wordpress.com/2008/08/14/todays-blogs/&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8867818" width="1" height="1"&gt;</description></item><item><title>Interesting Links – 8/14/2008</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8867339</link><pubDate>Thu, 14 Aug 2008 20:05:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8867339</guid><dc:creator>Matt Johnson's Technical Adventures</dc:creator><description>&lt;p&gt;Ask the Directory Services Team : MCS Talks Infrastructure Architecture joeware - never stop exploring…&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8867339" width="1" height="1"&gt;</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2008/08/12/8849519.aspx#8867056</link><pubDate>Thu, 14 Aug 2008 18:16:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8867056</guid><dc:creator>Andrew</dc:creator><description>
&lt;p&gt;[I believe most people expect reference semantics for iterators, not value semantics.]&lt;/p&gt;

&lt;p&gt;Vast majority of people writing vernacular C# use them in foreach loops. They never bother about ref/val semantics. Also, you palter a little bit: with a value type enumerator, in your example &lt;/p&gt;

&lt;p&gt;e = o.GetEnumerator();&lt;/p&gt;

&lt;p&gt;DoSomething1(e);&lt;/p&gt;

&lt;p&gt;DoSomething2(e);&lt;/p&gt;

&lt;p&gt;e gets boxed, so no difference between a class and a struct iterator. The only benefit for a value type iterator comes from the foreach loop: it uses duck typing against iterators, &amp;nbsp;thus doesn't cast to IEnumerator.&lt;/p&gt;

&lt;p&gt;[...I don't see how nesting is involved]&lt;/p&gt;

&lt;p&gt;Um, sorry - I meant recursion. If you &amp;nbsp;recursively interate through, all iterators (as far as I understand) should be on the stack that might, eg. cause stack overflow.&lt;/p&gt;
&lt;div class="post"&gt;[&lt;i&gt;You focused on boxing and missed the original problem. DoSomething2(e) will not resume where DoSomething1(e) left off. And stack overflow is not the issue here either. -Raymond&lt;/i&gt;]&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8867056" width="1" height="1"&gt;</description></item></channel></rss>