<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Extension Methods</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx</link><description>[Table of Contents] [Next Topic] Note: This article is a topic in a much larger tutorial on writing pure functional code in C#. Extension methods are only one of the pieces in a much larger puzzle. Functional programming in C# allows us to write shorter</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Command Connection Points and Exception Handling Reloaded</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#3782094</link><pubDate>Mon, 09 Jul 2007 17:25:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3782094</guid><dc:creator>Dennis "D.C." Dietrich</dc:creator><description>&lt;p&gt;In Debugging of and error handling for synchronous Commands I explained why you can't handle exceptions&lt;/p&gt;
</description></item><item><title>re: Extension Methods</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#8577426</link><pubDate>Fri, 06 Jun 2008 13:23:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8577426</guid><dc:creator>DKE</dc:creator><description>&lt;p&gt;If I get this right, the ability to implement extension methods on interfaces introduces some kind of (low-level) multiple-inheritance feature. You can implement extension methods for, say, two interfaces and let a certain class implement these two interfaces. Afterwards you have a class which is able to execute all(!) extension methods. Has nothing to do with functional programming, but really interesting in some way... What do you think about it?&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;namespace MultipleInheritance&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface I1 {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public interface I2 {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static class MyExtensions&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void Do1 ( this I1 obj ) { Console.WriteLine ( &amp;quot;Do1&amp;quot; ); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void Do2 ( this I2 obj ) { Console.WriteLine ( &amp;quot;Do2&amp;quot; ); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class MyClass : I1, I2 {}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;class Program&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main ( string[] args )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyClass obj = new MyClass();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;obj.Do1();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;obj.Do2();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Extension Methods</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#8578391</link><pubDate>Fri, 06 Jun 2008 19:36:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8578391</guid><dc:creator>EricWhite</dc:creator><description>&lt;P&gt;@DKE,&lt;/P&gt;
&lt;P&gt;Your point is interesting. &amp;nbsp;However, with multiple inheritance (MI) in C++, there is a different abstraction - MI means that a class has an 'is a' relationship to both of its parent classes. &amp;nbsp;The encapsulation semantics are different - a MI derived class has access to private members of both base classes. &amp;nbsp;In contrast, extension methods don't break encapusulation - you can't access private members.&lt;/P&gt;
&lt;P&gt;There is another concept in object oriented design, which is the idea of an extension class - you can extend an existing class with additional storage and methods. &amp;nbsp;(Think: add a object as an annotation to another object.) &amp;nbsp;There are no mainstream languages that implement this idea. &amp;nbsp;I've only seen proofs-of-concept prototype languages that do this. &amp;nbsp;Extension methods are a partial implementation of 'extension classes'. &amp;nbsp;When you have a general purpose mechanism such as annotations in LINQ to XML, then annotations+extension methods effectively equals extension classes, although the syntax isn't perfectly clean (but it's not bad). &amp;nbsp;I've also seen the idea of extension properties batted around. &amp;nbsp;But there are serious performance issues unless the properties are added as an annotation of some sort.&lt;/P&gt;
&lt;P&gt;So, my point of view is: it isn't really an implementation of an 'is a' relationship. &amp;nbsp;It is an approach to extending classes and interfaces, - a partial implementation of 'extension classes'.&lt;/P&gt;
&lt;P&gt;Interestingly, I have never encountered a class design problem where I absolutely needed to use multiple implementation inheritance. &amp;nbsp;So far, there have always been other ways to look at it, and other patterns to use that solve the designated problem without saying that one particular class has an 'is a' relationship to two parent classes.&amp;nbsp; Multiple interface inheritance is another matter - important for a variety of reasons.&lt;/P&gt;
&lt;P&gt;-Eric&lt;/P&gt;</description></item><item><title>Lambda Expressions</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#8787259</link><pubDate>Tue, 29 Jul 2008 08:24:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8787259</guid><dc:creator>Eric White's Blog</dc:creator><description>&lt;p&gt;[Table of Contents] [Next Topic] In order to learn functional programming and a more declarative style&lt;/p&gt;
</description></item><item><title>re: Extension Methods</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#8793295</link><pubDate>Thu, 31 Jul 2008 16:12:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8793295</guid><dc:creator>Kirk.B</dc:creator><description>&lt;p&gt;You say &amp;quot;There are times that writing extension methods on IEnumerable&amp;lt;T&amp;gt; is the way that you want do things.&amp;quot;&lt;/p&gt;
&lt;p&gt;But I don't see where. &amp;nbsp;Ther are so many already. &amp;nbsp;I sure would like an example. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Could be one later in the series... I haven't been through the whole thing.&lt;/p&gt;
</description></item><item><title>re: Extension Methods</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#8793352</link><pubDate>Thu, 31 Jul 2008 17:11:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8793352</guid><dc:creator>EricWhite</dc:creator><description>&lt;P&gt;@Kirk.B,&lt;/P&gt;
&lt;P&gt;There are two situations where I've used extension methods to good effect:&lt;/P&gt;
&lt;P&gt;1) Sometimes you want to add functionality to a sealed class. &amp;nbsp;Here are two examples:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/ericwhite/archive/2008/07/09/open-xml-sdk-and-linq-to-xml.aspx" target=_new rel=nofollow&gt;http://blogs.msdn.com/ericwhite/archive/2008/07/09/open-xml-sdk-and-linq-to-xml.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/ericwhite/archive/2006/08/31/734383.aspx" target=_new rel=nofollow&gt;http://blogs.msdn.com/ericwhite/archive/2006/08/31/734383.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;2) There is, for instance, grouping functionality that is difficult to get out of the standard extension methods:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/ericwhite/archive/2008/04/21/the-groupadjacent-extension-method.aspx" target=_new rel=nofollow&gt;http://blogs.msdn.com/ericwhite/archive/2008/04/21/the-groupadjacent-extension-method.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Extension methods are one of the syntactical constructs that allow us to gain composability.&amp;nbsp; Composability&amp;nbsp;allows us to embed / add / remove / inject / surround / refactor code with a cleaner syntax, and avoid brittleness at the same time.&lt;/P&gt;
&lt;P&gt;Does this make sense?&lt;/P&gt;
&lt;P&gt;-Eric&lt;/P&gt;</description></item><item><title>re: Extension Methods</title><link>http://blogs.msdn.com/ericwhite/pages/Extension-Methods.aspx#8831213</link><pubDate>Mon, 04 Aug 2008 18:07:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8831213</guid><dc:creator>Kirk.B</dc:creator><description>&lt;p&gt;Eric,&lt;/p&gt;
&lt;p&gt;Thanks for the links. &amp;nbsp;I especially appreciate the GroupAdjacent extension method.&lt;/p&gt;
</description></item></channel></rss>