{"id":94,"date":"2011-10-29T16:37:13","date_gmt":"2011-10-29T14:37:13","guid":{"rendered":"http:\/\/jcoder.me\/blog\/?p=94"},"modified":"2012-05-18T21:04:58","modified_gmt":"2012-05-18T19:04:58","slug":"c-extension-methods-part-2","status":"publish","type":"post","link":"https:\/\/jcoder.me\/blog\/2011\/10\/29\/c-extension-methods-part-2\/","title":{"rendered":"C# Extension Methods, Part 2"},"content":{"rendered":"<p>I&#8217;ve just made another tiny addition to my <a title=\"Continuous project: C# Extension Methods\" href=\"http:\/\/jcoder.me\/blog\/2011\/06\/30\/continuous-project-csharp-extension-methods\/\">extension method class \/ project<\/a>, adding a small, but maybe useful method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Repeats this IEnumerable a given number of times\r\n\/\/\/ &lt;\/summary&gt;\r\n\/\/\/ &lt;param name=&quot;enumerable&quot;&gt;this IEnumerable&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;times&quot;&gt;number of times to repeat this IEnumerable&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;IEnumerable&lt;\/returns&gt;\r\npublic static IEnumerable&lt;T&gt; Repeated&lt;T&gt;(this IEnumerable&lt;T&gt; enumerable, int times)\r\n{\r\n    if (times &lt; 1)\r\n    {\r\n        yield break;\r\n    }\r\n    for (int i = 0; i &lt; times; i++)\r\n    {\r\n        foreach (T oneObject in enumerable)\r\n        {\r\n            yield return oneObject;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>This is how to use it to consume an IEnumerable&lt;T&gt; multiple times:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar range = Enumerable.Range(1, 5);\r\nforeach (var i in range.Repeated(5))\r\n{\r\n    \/\/ doing something with i, which will be the sequence\r\n    \/\/ from 1 to 5, repeated 5 times\r\n}\r\n<\/pre>\n<p>It&#8217;s not a big deal, but maybe it&#8217;s helpful anyway. Btw, the <a href=\"http:\/\/blog.jcoder.me\/files\/csharp\/IEnumerableExtensions.cs\" target=\"_blank\">source code<\/a> is updated, too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just made another tiny addition to my extension method class \/ project, adding a small, but maybe useful method: \/\/\/ &lt;summary&gt; \/\/\/ Repeats this IEnumerable a given number of times \/\/\/ &lt;\/summary&gt; \/\/\/ &lt;param name=&quot;enumerable&quot;&gt;this IEnumerable&lt;\/param&gt; \/\/\/ &lt;param name=&quot;times&quot;&gt;number &hellip; <a href=\"https:\/\/jcoder.me\/blog\/2011\/10\/29\/c-extension-methods-part-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,1,28,11],"tags":[8,14],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-coding","category-common","category-extension-methods","category-project","tag-c","tag-code-download"],"_links":{"self":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":3,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}