<?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/oldnewthing/archive/2008/08/12/8849519.aspx</link><description>A secret state machine.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8851561</link><pubDate>Tue, 12 Aug 2008 17:26:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8851561</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Because weird things might happen with MoveNext() if it's called more than once?&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8851570</link><pubDate>Tue, 12 Aug 2008 17:27:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8851570</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;(...Past the end of the enumerated collection?)&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8851583</link><pubDate>Tue, 12 Aug 2008 17:30:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8851583</guid><dc:creator>Damien</dc:creator><description>&lt;p&gt;Because whatever state it was in might have a non-deterministic path that sometimes leads to a yield break, and other times to a yield return?&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8851622</link><pubDate>Tue, 12 Aug 2008 17:36:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8851622</guid><dc:creator>Damien</dc:creator><description>&lt;p&gt;Ooh. Plus, whatever code is in the last but one state might have side effects (might be what Chris was implying)&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8852087</link><pubDate>Tue, 12 Aug 2008 19:09:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8852087</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;If you don't change the state to n2, you can keep calling MoveNext() on it and always redo the last state.&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8852165</link><pubDate>Tue, 12 Aug 2008 19:23:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8852165</guid><dc:creator>bahbar</dc:creator><description>&lt;p&gt;I believe Damien and Chris nailed it.&lt;/p&gt;
&lt;p&gt;MoveNext is supposed to keep returning false once it started to do so. &lt;/p&gt;
&lt;p&gt;From MSDN &amp;quot;If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false&amp;quot;&lt;/p&gt;
&lt;p&gt;Imagine the for loop condition is &amp;nbsp;i != limit rather than &amp;lt;=... Then MoveNext will return false then start to return true again.&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8852359</link><pubDate>Tue, 12 Aug 2008 19:59:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8852359</guid><dc:creator>hova</dc:creator><description>&lt;p&gt;What do you plan to do with all the excercises when you write your next book? &amp;nbsp; Put a workbook or test section at the end of each chapter?&lt;/p&gt;
&lt;p&gt;Why some teacher might even use that book to teach students with!&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8852572</link><pubDate>Tue, 12 Aug 2008 20:39:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8852572</guid><dc:creator>Michael</dc:creator><description>&lt;p&gt;&amp;gt; The C# method ... just borrows the stack of the caller&lt;/p&gt;
&lt;p&gt;Interesting... When I first saw how iterators were implemented in C#, I saw the &amp;quot;yield&amp;quot; and immediately assumed coroutines. &amp;nbsp;So, I'm surprised to learn .NET iterators are really closer to Java iterator objects than &amp;quot;true&amp;quot; (coroutine-based or thread-based) iterators; it's just that the nice syntax of C# hides this from us.&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8853028</link><pubDate>Tue, 12 Aug 2008 22:06:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8853028</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Indeed. You can use iterators as poor-mans coroutines; in fact, the managed robotics framework that Microsoft ships does exactly that!&lt;/p&gt;
&lt;p&gt;Thanks for doing this Raymond; this is better than our internal documentation. :-) &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8853599</link><pubDate>Wed, 13 Aug 2008 00:02:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8853599</guid><dc:creator>philq</dc:creator><description>&lt;p&gt;Iterators are a really great feature. &amp;nbsp;The only drawback is that making recursive calls in them is n^2. &amp;nbsp;We need a 'yield foreach' statement! &amp;nbsp;Eric please implement this feature immediately.&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8853676</link><pubDate>Wed, 13 Aug 2008 00:19:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8853676</guid><dc:creator>Ajai Shankar</dc:creator><description>&lt;p&gt;Hi Raymond&lt;/p&gt;
&lt;p&gt;Strange I came across only now across this series on enumerators &amp;amp; fibers.&lt;/p&gt;
&lt;p&gt;I'd written an MSDN article way back in '03 on how to get fiber based enumerators to work with .NET using an undocumented hosting API.&lt;/p&gt;
&lt;p&gt;Implementing Coroutines for .NET by Wrapping the Unmanaged Fiber API (&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/magazine/cc164086.aspx"&gt;http://msdn.microsoft.com/en-us/magazine/cc164086.aspx&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Of course now there is a dire warning at the beginning of the article saying it is not supported by Microsoft :-)&lt;/p&gt;
&lt;p&gt;Ajai Shankar&lt;/p&gt;
</description></item><item><title>Not actually a .Net blog.</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8854410</link><pubDate>Wed, 13 Aug 2008 02:28:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8854410</guid><dc:creator>Igor Levicki</dc:creator><description>&lt;P&gt;Not actually a .Net blog but it might occasionally dip its toes in C# it seems.&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;&lt;A href="http://blogs.msdn.com/oldnewthing/archive/2007/08/13/4358659.aspx"&gt;Why is the blog's subtitle "Not actually a .NET blog"?&lt;/A&gt; -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>
	Iterators and the yield statement - Rory Primrose
</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8854592</link><pubDate>Wed, 13 Aug 2008 03:00:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8854592</guid><dc:creator>
	Iterators and the yield statement - Rory Primrose
</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://neovolve.com/archive/2008/08/13/iterators-and-the-yield-statement.aspx"&gt;http://neovolve.com/archive/2008/08/13/iterators-and-the-yield-statement.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8854638</link><pubDate>Wed, 13 Aug 2008 03:09:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8854638</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;.net week makes the baby jesus cry.&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8855372</link><pubDate>Wed, 13 Aug 2008 05:29:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8855372</guid><dc:creator>Miral</dc:creator><description>&lt;p&gt;My favourite iterator trick is the synchronised foreach:&lt;/p&gt;
&lt;p&gt;public IEnumerator&amp;lt;T&amp;gt; GetEnumerator()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;lock (SyncRoot)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; foreach (T item in BaseList)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return item;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;(Not 100% sure that's the right code; I did it from memory.)&lt;/p&gt;
&lt;p&gt;Of course there are drawbacks to doing something like the above, but it *does* mean that your foreach loops on that collection won't get borked by other threads modifying the collection. &amp;nbsp;(It might kill your performance, though.)&lt;/p&gt;
&lt;p&gt;My second favourite trick is the way you can now define multiple kinds of enumeration trivially:&lt;/p&gt;
&lt;p&gt;// .. in some grid/matrix-like class:&lt;/p&gt;
&lt;p&gt;public IEnumerable&amp;lt;T&amp;gt; AcrossThenDown&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;for (int rowIndex = 0; rowIndex &amp;lt; RowCount; ++rowIndex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int colIndex = 0; colIndex &amp;lt; ColCount; ++colIndex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return this[row, col];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public IEnumerable&amp;lt;T&amp;gt; DownThenAcross&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;get&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;for (int colIndex = 0; colIndex &amp;lt; ColCount; ++colIndex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int rowIndex = 0; rowIndex &amp;lt; RowCount; ++rowIndex)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return this[row, col];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8855385</link><pubDate>Wed, 13 Aug 2008 05:33:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8855385</guid><dc:creator>Miral</dc:creator><description>&lt;p&gt;Gah, stuffed that last example up a bit (row =&amp;gt; rowIndex, col =&amp;gt; colIndex), and the formatting sucks. &amp;nbsp;Still, you get the idea :) &amp;nbsp;(Oh, for an edit button...)&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8856884</link><pubDate>Wed, 13 Aug 2008 11:22:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8856884</guid><dc:creator>wtroost</dc:creator><description>&lt;p&gt;Thanks for the great post, I always wondered how the yield statement worked its magic!&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8856995</link><pubDate>Wed, 13 Aug 2008 11:55:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8856995</guid><dc:creator>SRS</dc:creator><description>&lt;p&gt;I really wish Sun would put 'yield' into Java. Oh, and fix generics.&lt;/p&gt;
&lt;p&gt;Just saying.&lt;/p&gt;
</description></item><item><title>A closer look at yield – part 3</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8857939</link><pubDate>Wed, 13 Aug 2008 15:37:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8857939</guid><dc:creator>Stuart Leeks</dc:creator><description>&lt;p&gt;This was only going to be two posts, but after my last post I’d been mulling over a post that looks at&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8858678</link><pubDate>Wed, 13 Aug 2008 17:49:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8858678</guid><dc:creator>Rob</dc:creator><description>&lt;P&gt;Shouldn't public IEnumerator&amp;lt;int&amp;gt; CountFrom(int start) [fourth line]&lt;/P&gt;
&lt;P&gt;be &lt;/P&gt;
&lt;P&gt;public IEnumerable&amp;lt;int&amp;gt; CountFrom(int start) &lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;DIV class=post&gt;[&lt;I&gt;Fixed. Thanks. -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8865590</link><pubDate>Thu, 14 Aug 2008 11:12:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8865590</guid><dc:creator>Ian Marteens</dc:creator><description>&lt;p&gt;Actually, the translation get trickier when there are try/finally blocks, either explicit or implicit, in the iterator block. That's because you must track iteration state in case the Dispose method of the enumerator must be called. That's also the reason why try/catch is not allowed in iterators: translation becomes even trickier.&lt;/p&gt;
&lt;p&gt;There are a couple of enhancements that Microsoft could apply to iterators:&lt;/p&gt;
&lt;p&gt;1- With a good peephole optimizer, the target code generated for the compiler can be greatly simplified. Most compiled code have a lot of redundant state assignments that a peephole optimizer could erase. I have done some tests on this with Freya, my own pet language and compiler for .NET.&lt;/p&gt;
&lt;p&gt;2- A very simple extension for iterators: allow &amp;quot;yield&amp;quot; to receive a whole iterator call. It would translate as a foreach/yield combo statement, and it would be great for composing iterators, such as in recursive iterators. For simpler composite iterators, we have LINQ, of course.&lt;/p&gt;
&lt;p&gt;3- I have read a paper from Microsoft Research where they fiddle with a IEnumerable extension for handling recursive iterators in an efficient manner. It would be great to see that research applied to the .NET compilers. Of course, it's not an easy task.&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8866114</link><pubDate>Thu, 14 Aug 2008 13:07:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8866114</guid><dc:creator>Andrew</dc:creator><description>
&lt;p&gt;"...Instead it just borrows the stack of the caller, and anything that it needs to save across calls to MoveNext are stored in a helper object (which goes on the heap rather than the stack)..."&lt;/p&gt;

&lt;p&gt;I used to think that implementation of an iterator as a value type is more efficient in terms of performance (say, generic list and dictionary have their iterators as structs).&lt;/p&gt;

&lt;p&gt;And I used to think that "yield return" &amp;nbsp;just fails to expose a struct since that should be public (to aviod boxing) and that's not appropriate for autogenerated classes.&lt;/p&gt;

&lt;p&gt;But Raymond seems to say that heap-based iterators are better, why? Because of possible nesting?&lt;/p&gt;
&lt;div class="POST"&gt;[&lt;i&gt;I believe most people expect reference semantics for iterators, not value semantics.
&lt;pre&gt;e = o.GetEnumerator();
DoSomething1(e);
DoSomething2(e);
&lt;/pre&gt;
I suspect most people would expect &lt;code&gt;DoSomething2&lt;/code&gt; to continue where &lt;code&gt;DoSomething1&lt;/code&gt; left off. I don't see how nesting is involved. -Raymond&lt;/i&gt;]&lt;/div&gt;</description></item><item><title>Visual Studio Links #65</title><link>http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx#8866710</link><pubDate>Thu, 14 Aug 2008 16:28:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8866710</guid><dc:creator>Visual Studio Hacks</dc:creator><description>&lt;p&gt;My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. The Visual C++ Team blog has a post about IconWorkshop Lite , which is available as a free download. Greg Duncan posted a link to&lt;/p&gt;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/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;</description></item><item><title>Interesting Links – 8/14/2008</title><link>http://blogs.msdn.com/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;
</description></item><item><title>Today&amp;#8217;s Blogs &amp;laquo; Roman&amp;#8217;s Blog</title><link>http://blogs.msdn.com/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;
</description></item><item><title>Iterators</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/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;
</description></item><item><title>re: The implementation of iterators in C# and its consequences (part 1)</title><link>http://blogs.msdn.com/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;
</description></item><item><title>Tricky IEnumerable&lt;T&gt;</title><link>http://blogs.msdn.com/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;
</description></item><item><title>Haugerns Development Escapades  &amp;raquo; Blog Archive   &amp;raquo; Beware of the IEnumerables</title><link>http://blogs.msdn.com/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;
</description></item><item><title>C# Yield Keyword &amp;laquo; Vasu Balakrishnan&amp;#8217;s Blog</title><link>http://blogs.msdn.com/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;
</description></item></channel></rss>