{"id":21223,"date":"2008-08-15T10:00:00","date_gmt":"2008-08-15T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/08\/15\/the-implementation-of-iterators-in-c-and-its-consequences-part-4\/"},"modified":"2008-08-15T10:00:00","modified_gmt":"2008-08-15T10:00:00","slug":"the-implementation-of-iterators-in-c-and-its-consequences-part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080815-00\/?p=21223","title":{"rendered":"The implementation of iterators in C# and its consequences (part 4)"},"content":{"rendered":"<p>You can breathe a sigh of relief. <a href=\"http:\/\/www.ford.utexas.edu\/LIBRARY\/speeches\/740001.htm\"> Our long national nightmare is over<\/a>: <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/08\/13\/8854601.aspx#8858540\"> this is the end of CLR&nbsp;Week&nbsp;2008<\/a>. We wind down with a look back at iterators.<\/p>\n<p> <a href=\"http:\/\/blogs.msdn.com\/michen\/\"> Michael Entin<\/a> points out that <a href=\"http:\/\/blogs.msdn.com\/michen\/archive\/2006\/03\/30\/564671.aspx\"> you can use C# iterators to make asynchronous code easier to write<\/a>. You can use C# iterators for more than simply iterating. <\/p>\n<p> The automatic conversion of straight line code into a state machine is handy when you want an easy way to write, well, a state machine. It&#8217;s one of those things that&#8217;s blindingly obvious once you look at it the right way. <\/p>\n<p> The transformation that the <code>yield return<\/code> statement induces on your function turns it from a boring function into an implicit state machine: When you execute a <code>yield return<\/code>, execution of your function is suspended until somebody asks your iterator the next item, at which point execution resumes at the statement after the <code>yield return<\/code>. This is exactly what you want when breaking a synchronous function into asynchronous pieces: Each time you would normally block on an operation, you instead perform a <code>yield return<\/code>, and when the operation completes, you call the <code>MoveNext<\/code> method, which resumes execution of the function until the next time it needs to wait for something and performs a <code>yield return<\/code>. <\/p>\n<p> It&#8217;s so simple it&#8217;s magic. <\/p>\n<p> Additional iterator-related reading: <\/p>\n<ul>\n<li>     <a href=\"http:\/\/blogs.msdn.com\/dancre\/\">     Dan Crevier<\/a>     investigates a problem where     <a href=\"http:\/\/blogs.msdn.com\/dancre\/archive\/2008\/03\/14\/yield-and-usings-your-dispose-may-not-be-called.aspx\">     an enumerator&#8217;s <code>Dispose<\/code> method was never called<\/a>. <\/li>\n<li>     <a href=\"http:\/\/blogs.msdn.com\/stuartleeks\/\">     Stuart Leeks<\/a>     has his own series on the <code>yield<\/code> keyword:     <a href=\"http:\/\/blogs.msdn.com\/stuartleeks\/archive\/2008\/07\/14\/a-closer-look-at-yield.aspx\">     Part 1<\/a>,     <a href=\"http:\/\/blogs.msdn.com\/stuartleeks\/archive\/2008\/07\/15\/a-closer-look-at-yield-part-2.aspx\">     Part 2<\/a>. <\/li>\n<li>     <a href=\"http:\/\/blogs.msdn.com\/ericlippert\/\">     Eric Lippert<\/a>     <a href=\"http:\/\/blogs.msdn.com\/ericlippert\/archive\/2007\/09\/05\/psychic-debugging-part-one.aspx\">     invites you to use your psychic debugging powers<\/a>     to diagnose what was originally reported as a compiler bug.     You in fact already know enough to solve this.     (<a href=\"http:\/\/blogs.msdn.com\/ericlippert\/archive\/2007\/09\/06\/psychic-debugging-part-two.aspx\">Answer<\/a>.) <\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You can breathe a sigh of relief. Our long national nightmare is over: this is the end of CLR&nbsp;Week&nbsp;2008. We wind down with a look back at iterators. Michael Entin points out that you can use C# iterators to make asynchronous code easier to write. You can use C# iterators for more than simply iterating. [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-21223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can breathe a sigh of relief. Our long national nightmare is over: this is the end of CLR&nbsp;Week&nbsp;2008. We wind down with a look back at iterators. Michael Entin points out that you can use C# iterators to make asynchronous code easier to write. You can use C# iterators for more than simply iterating. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=21223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/21223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=21223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=21223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=21223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}