{"id":39,"date":"2011-06-30T21:34:52","date_gmt":"2011-06-30T19:34:52","guid":{"rendered":"http:\/\/jcoder.me\/blog\/?p=39"},"modified":"2012-05-18T21:05:20","modified_gmt":"2012-05-18T19:05:20","slug":"continuous-project-csharp-extension-methods","status":"publish","type":"post","link":"https:\/\/jcoder.me\/blog\/2011\/06\/30\/continuous-project-csharp-extension-methods\/","title":{"rendered":"Continuous project: C# Extension Methods"},"content":{"rendered":"<p>There are a lot of cool features in the current C# version (and I&#8217;m sure that I will cover most of them in future postings), but one of the features I like most are <strong>Extension Methods<\/strong>.<\/p>\n<p>If you&#8217;ve never heard about Extension Methods, let me cite the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb383977(v=VS.90).aspx\" target=\"_blank\">MSDN documentation on Extension Methods<\/a>:<\/p>\n<blockquote><p>Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.<\/p><\/blockquote>\n<p>Extension Methods are a very powerful feature when it comes to writing easy-to-understand code. So, over the course of the weeks and months I&#8217;ll post code fragments with (mostly) helpful Extension Methods, at least if they&#8217;re helpful for me. I will also upload the latest source code files to my blog.<\/p>\n<p>Unless otherwise stated the code is provided as &#8220;Public Domain&#8221; which means that you can modify and use it in all your projects.<\/p>\n<p>So, let&#8217;s get started with the first methods.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Performs an action with each of the elements in this IEnumerable\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;action&quot;&gt;action to be performed on each element&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;IEnumerable&lt;\/returns&gt;\r\npublic static IEnumerable&lt;T&gt; WithEach&lt;T&gt;(this IEnumerable&lt;T&gt; enumerable, Action&lt;T&gt; action) {\r\n\tif (action == null) {\r\n\t\taction = (obj) =&gt; {};\r\n\t}\r\n\tforeach (T oneObject in enumerable) {\r\n\t\taction(oneObject);\r\n\t\tyield return oneObject;\r\n\t}\r\n}\r\n\r\n\/\/\/ &lt;summary&gt;\r\n\/\/\/ Concatinates all elements of this IEnumerable to a flat string, optionally quoting them\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;glue&quot;&gt;glue between the elements&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;quoteStart&quot;&gt;quote at start of each element&lt;\/param&gt;\r\n\/\/\/ &lt;param name=&quot;quoteEnd&quot;&gt;quote at end of each element&lt;\/param&gt;\r\n\/\/\/ &lt;returns&gt;flat string&lt;\/returns&gt;\r\npublic static string ToFlatString&lt;T&gt;(this IEnumerable&lt;T&gt; enumerable, string glue, string quoteStart, string quoteEnd) {\r\n\tif (glue == null) {\r\n\t\tglue = &quot;,&quot;;\r\n\t}\r\n\tif (quoteStart == null) {\r\n\t\tquoteStart = &quot;&quot;;\r\n\t}\r\n\tif (quoteEnd == null) {\r\n\t\tquoteEnd = &quot;&quot;;\r\n\t}\r\n\tStringBuilder strBuilder = new StringBuilder();\r\n\tbool useGlue = false;\r\n\tforeach (T oneObject in enumerable) {\r\n\t\tif (useGlue == true) {\r\n\t\t\tstrBuilder.Append(glue);\r\n\t\t} else {\r\n\t\t\tuseGlue = true;\r\n\t\t}\r\n\t\tstrBuilder.Append(quoteStart).Append(oneObject).Append(quoteEnd);\r\n\t}\r\n\treturn strBuilder.ToString();\r\n}\r\n<\/pre>\n<p>But wait, there&#8217;s more ;)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static string ToFlatString&lt;T&gt;(this IEnumerable&lt;T&gt; enumerable, string glue)\r\npublic static string ToFlatString&lt;T&gt;(this IEnumerable&lt;T&gt; enumerable, string glue, Func&lt;T, string&gt; toStringFnc)\r\n<\/pre>\n<p>As don&#8217;t want this posting to be just code, I&#8217;ve only outlined the additional method signatures here, so feel free to download the complete file from <a href=\"http:\/\/blog.jcoder.me\/files\/csharp\/IEnumerableExtensions.cs\" target=\"_blank\">http:\/\/blog.jcoder.me\/files\/csharp\/IEnumerableExtensions.cs<\/a><\/p>\n<p>If you have any questions or ideas for useful Extension Methods, please leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a lot of cool features in the current C# version (and I&#8217;m sure that I will cover most of them in future postings), but one of the features I like most are Extension Methods. If you&#8217;ve never heard &hellip; <a href=\"https:\/\/jcoder.me\/blog\/2011\/06\/30\/continuous-project-csharp-extension-methods\/\">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-39","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\/39","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=39"}],"version-history":[{"count":6,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}