{"id":3046,"date":"2010-04-23T17:06:18","date_gmt":"2010-04-23T17:06:18","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/thoughts-on-c-extension-methods\/"},"modified":"2016-07-28T10:49:58","modified_gmt":"2016-07-28T10:49:58","slug":"thoughts-on-c-extension-methods","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/thoughts-on-c-extension-methods\/","title":{"rendered":"Thoughts on C# Extension Methods"},"content":{"rendered":"<p>I&#8217;m not a huge fan of extension methods.&#160; When they first came out, I remember seeing a method on an object that was fairly useful, but when I went to use it another piece of code that method wasn&#8217;t available.&#160; Turns out it was an extension method and I hadn&#8217;t included the appropriate assembly and imports statement in my code to use it.&#160; I remember being a bit confused at first about how the heck that could happen (hey, extension methods were new, cut me some slack) and it took a bit of time to track down exactly what it was that I needed to include to get that method back.&#160; I just imagined a new developer trying to figure out why a method was missing and fruitlessly searching on MSDN for a method that didn&#8217;t exist and it just didn&#8217;t sit well with me.<\/p>\n<p>I am of the opinion that if you have an object, then you shouldn&#8217;t have to include additional assemblies to get additional instance level methods out of that object.&#160; That opinion applies to namespaces as well &#8211; I do not like it when the contents of a namespace are split out into multiple assemblies.&#160; I prefer to have static utility classes instead of extension methods to keep things nicely packaged into a cohesive unit.&#160; It also makes it abundantly clear where utility methods are used in code.&#160; I will concede, however, that it can make code a bit more verbose and lengthy.&#160; There is always a trade-off.<\/p>\n<p>Some people harp on extension methods because it breaks the tenants of object oriented development and allows you to add methods to sealed classes.&#160; Whatever.&#160; Extension methods are just utility methods that you can tack onto an object after the fact.&#160; Extension methods do not give you any more access to an object than the developer of that object allows, so I say that those who cry OO foul on extension methods really don&#8217;t have much of an argument on which to stand.&#160; In fact, I have to concede that my dislike of them is really more about style than anything of great substance.<\/p>\n<p>One interesting thing that I found regarding extension methods is that you can call them on null objects. Take a look at this extension method:    <br \/><code>       <br \/>namespace ExtensionMethods        <br \/>{         <br \/>&#160; public static class StringUtility        <br \/>&#160; {         <br \/>&#160;&#160;&#160; public static int WordCount(this string str)        <br \/>&#160;&#160;&#160; {        <br \/>&#160;&#160;&#160;&#160;&#160; if(str == null) return 0;        <br \/>&#160;&#160;&#160;&#160;&#160; return str.Split(new char[] { ' ', '.', '?' },        <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; StringSplitOptions.RemoveEmptyEntries).Length;        <br \/>&#160;&#160;&#160; }        <br \/>&#160; }&#160;&#160; <br \/>}<\/code><\/p>\n<p>Notice that the extension method checks to see if the incoming string parameter is null.&#160; I was worried that the runtime would perform a check on the object instance to make sure it was not null before calling an extension method, but that is apparently not the case.&#160; So, if you call the following code it runs just fine.<\/p>\n<p> <code>string s = null;     <br \/>int words = s.WordCount();<\/code>   <\/p>\n<p>I am a big fan of things working, but this seems to go against everything I&#8217;ve come to know about instance level methods.&#160; However, an extension method is really a static method masquerading as an instance-level method, so I suppose it would be far more frustrating if it failed since there is really no reason it shouldn&#8217;t succeed.<\/p>\n<p>Although I&#8217;m not a fan of extension methods, I will say that if you ever find yourself at an impasse with a die-hard fan of either the utility class or extension method approach, then there is a common ground.&#160; Extension methods are defined in static classes, and you call them from those static classes as well as directly from the objects they extend.&#160; So if you build your utility classes using extension methods, then you can have it your way and they can have it theirs.&#160; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m not a huge fan of extension methods.&#160; When they first came out, I remember seeing a method on an object that was fairly useful, but when I went to use it another piece of code that method wasn&#8217;t available.&#160; Turns out it was an extension method and I hadn&#8217;t included the appropriate assembly and&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-3046","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3046"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3046\/revisions"}],"predecessor-version":[{"id":41885,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3046\/revisions\/41885"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3046"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}