{"id":68,"date":"2008-10-21T18:26:37","date_gmt":"2008-10-21T16:26:37","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=68"},"modified":"2009-05-16T10:01:47","modified_gmt":"2009-05-16T08:01:47","slug":"yield-and-return","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=68","title":{"rendered":"Yield and Return"},"content":{"rendered":"<p>No, this is not a post about the current state of the worlds financial industries, instead it is about one of the lesser known C# features called &#8220;yield&#8221; that can be used to create dynamic collections. It is one of the more disputed features of C# because it is &#8220;not really object oriented&#8221; and more of a hack than anything else. While this is most likely true, I find it hard to argue its usability. See for yourself.<br \/>\n<!--more--><\/p>\n<p>The <code>yield return<\/code> and <code>yield break<\/code> keywords are only valid in methods and properties with a IEnumerable return type. They can be used to generate object enumerators &#8220;on the fly&#8221; without the need to explicitly create a temporary container. <code>yield return obj;<\/code> will add <code>obj<\/code> to the imaginary collection and <code>yield break;<\/code> will terminate the enumeration. The example code below shows how to use the yield mechanism to iterate over a dynamically generated collection.<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; toolbar: false; wrap-lines: false; notranslate\" title=\"\">\r\npublic IEnumerable PowersOfTwo(int upperBound)\r\n{\r\n    int current = 1;\r\n\r\n    while (current &lt; upperBound)\r\n    {\r\n        yield return current;\r\n        current *= 2;\r\n    }\r\n\r\n    yield break;\r\n}\r\n\r\npublic void ShowPowersOfTwo()\r\n{\r\n    foreach (int num in PowersOfTwo(1000000))\r\n    {\r\n        Console.WriteLine(num);\r\n    }\r\n}\r\n<\/pre>\n<p>The output of which would of course be<\/p>\n<pre>1\r\n2\r\n4\r\n8\r\n16\r\n...<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>No, this is not a post about the current state of the worlds financial industries, instead it is about one of the lesser known C# features called &#8220;yield&#8221; that can be used to create dynamic collections. It is one of the more disputed features of C# because it is &#8220;not really object oriented&#8221; and more &hellip; <\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[45,46],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-executor","4":"has-more-link","5":"post-68","7":"format-standard","8":"category-csharp","9":"post_tag-feature","10":"post_tag-on-the-fly"},"acf":[],"views":1601,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/68"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}