<?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>Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx</link><description>Here's some back-and-forth from an email conversation I had with a user a while back. 
 Why should one avoid method hiding? 
 If there were no advantages and only disadvantages then we would not have added it to the language in the first place. C# implements</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#10139089</link><pubDate>Thu, 10 Mar 2011 06:37:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10139089</guid><dc:creator>Jeremy S. Pederson</dc:creator><description>&lt;p&gt;I personally like this feature... backwards compatibility&lt;/p&gt;
&lt;p&gt;//old communication object&lt;/p&gt;
&lt;p&gt;internal class CommunicationObject : ICommunicationObject&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public CommunicationObject(ISomeInterface someInterface)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.ISomeInterface = someInterface;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;protected ISomeInterface { get; private set; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;//new, more advanced, communication object&lt;/p&gt;
&lt;p&gt;internal class CommunicationObjectEx : CommunicationObject, ICommunicationObjectEx&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public CommunicationObjectEx(ISomeInterfaceEx someInterface)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: base(someInterface) { &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;new protected ISomeInterfaceEx SomeInterface&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; get { return base.SomeInterface as ISomeInterfaceEx; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;//public facing factory&lt;/p&gt;
&lt;p&gt;public static CommunicationObjectFactory&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;//old factory method&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static ICommunicationObject CreateCommunicationObject()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new CommunicationObjectEx(new SomeClassEx());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;//new factory method that lets the caller initialize a new version of the communication object without breaking old code&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static ICommunicationObjectEx CreateCommunicationObjectEx()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new CommunicationObjectEx(new SomeClassEx());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10139089" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#9856287</link><pubDate>Mon, 03 Aug 2009 17:59:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9856287</guid><dc:creator>Ralph Compton</dc:creator><description>&lt;p&gt;I still don't see why the language includes both method overriding and hiding. Can this be explained without a reference to covariance?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9856287" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#9433461</link><pubDate>Thu, 19 Feb 2009 10:37:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9433461</guid><dc:creator>Brian Russo</dc:creator><description>&lt;p&gt;I think this is a fairly decent article.. but frankly I'm not sold on method hiding at all.&lt;/p&gt;
&lt;p&gt;I understand there could be situations where one can make use of method hiding; but it seems pretty non-intuitive by breaking how people expect polymorphism and your API to work.&lt;/p&gt;
&lt;p&gt;I won't say I'll never use it.. but I hope I never do; because it seems that if you're having to do a lot of funky method hiding then your object hierarchy is probably pretty hosed.&lt;/p&gt;
&lt;p&gt;Bottom line I just see very little advantage to using method hiding over method overriding. The latter is much more intuitive and is how most people expect things to work. The examples given are relatively simple yet still somewhat confusing - I don't even want to think how much damage you could do by hiding methods in larger chunks of code.&lt;/p&gt;
&lt;p&gt;cheers,&lt;/p&gt;
&lt;p&gt; - bri&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9433461" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#9043758</link><pubDate>Wed, 05 Nov 2008 12:06:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9043758</guid><dc:creator>Nick</dc:creator><description>&lt;p&gt;Personally, programming is not about creating frankenstein concepts. This cake example in my view demonstrates exactly how not to use these concepts. &amp;nbsp;Consider the scenario when a programmer (possibly new to the project) is tasked with calculating tax on a bunch of items, and creates a list of generic groceries, only to calculate the wrong tax because of the frankenstein relationships. &amp;nbsp;Programming is hard enough right?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9043758" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#8773082</link><pubDate>Sat, 26 Jul 2008 00:29:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8773082</guid><dc:creator>David Nelson</dc:creator><description>&lt;p&gt;@Konstantin,&lt;/p&gt;
&lt;p&gt;The reason IEnumerable&amp;lt;T&amp;gt; needs to implement IEnumerable but doesn't need to worry about implementing IEnumerable&amp;lt;object&amp;gt; is so that a reference to IEnumerable&amp;lt;T&amp;gt; can be used with code that pre-dates generics. For example:&lt;/p&gt;
&lt;p&gt;public void OldFoo(IEnumerable bars)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;// ...&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public void NewFoo&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; bars)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; // This doesn't work if IEnumerable&amp;lt;T&amp;gt; doesn't implement IEnumerable&lt;/p&gt;
&lt;p&gt; &amp;nbsp; OldFoo(bars);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;While you could say the same thing about IEnumerable&amp;lt;object&amp;gt;, there are well known ways around that problem (using generic methods instead of IEnumerable&amp;lt;object&amp;gt;, for example), whereas with pre-generic code the only workaround is writing a wrapper class. Also, generic covariance, if it ever gets added to the language, will take care of the IEnumerable&amp;lt;object&amp;gt; problem, whereas it can't take care of the IEnumerable problem since they are two different types.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8773082" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#8685274</link><pubDate>Thu, 03 Jul 2008 22:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8685274</guid><dc:creator>Alex Grenier</dc:creator><description>&lt;p&gt;To me, the usefulness of hiding is to change the result of a method call based on which subtype it gets casted to. This is a good way to &amp;quot;downgrade specialization&amp;quot;. Think of a scenario where such a type gets passed to methods where the parameter may be defined as type A or type B - thus altering the behavior of the passed object.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8685274" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#8675738</link><pubDate>Tue, 01 Jul 2008 10:23:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8675738</guid><dc:creator>Konstantin Triger</dc:creator><description>&lt;p&gt;To Joshua:&lt;/p&gt;
&lt;p&gt;Thanks for your comments!&lt;/p&gt;
&lt;p&gt;I think all this thread is a &amp;quot;post mortem&amp;quot; analysis. Clearly we won't be able to remove this feature. Yet we want understand whether it was the right thing to do, so we will be able to recommend better practices, add FxCop rules etc.&lt;/p&gt;
&lt;p&gt;The main point of my post is that Extension methods could give a reasonable solution to the problem, if and only if the compiler would make them to precede in name resolution chain, i.e.:&lt;/p&gt;
&lt;p&gt;class Y {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void test() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Y&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;class X : Y {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void testEx() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;X&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;//declared by the user in &amp;nbsp;the user code!&lt;/p&gt;
&lt;p&gt;static class ExtensionX {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static void test(this X x) &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x.testEx();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;new X().test(); // -&amp;gt; 'X' is printed (currently the output is 'Y')&lt;/p&gt;
&lt;p&gt;So the user will be able to call a 'simpler' extension method.&lt;/p&gt;
&lt;p&gt;About my and your #3 P.S.: Following your logic IEnumerable&amp;lt;T&amp;gt; should be also IEnumerable&amp;lt;object&amp;gt;, which is not the case. If due to some reason you need to convert your IEnumerable&amp;lt;T&amp;gt; to IEnumerable, one way would be a method like this:&lt;/p&gt;
&lt;p&gt;public static IEnumerable ToGenericIEnumerable&amp;lt;T&amp;gt;(IEnumerable&amp;lt;T&amp;gt; e) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;foreach (T t in e)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return t;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Of course that's not needed, since IEnumerable&amp;lt;T&amp;gt; derives from IEnumerable, but what would you do if you needed to convert IEnumerable&amp;lt;T&amp;gt; to IEnumerable&amp;lt;X&amp;gt;, provided T derives from X?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8675738" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#8671954</link><pubDate>Mon, 30 Jun 2008 20:37:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8671954</guid><dc:creator>Joshua</dc:creator><description>&lt;p&gt;Konstantin Triger,&lt;/p&gt;
&lt;p&gt;Method hiding is already part of the C# specification, so it is here to stay unless it proves to be used by no one. (Since the CLR uses it, that is not likely)&lt;/p&gt;
&lt;p&gt;Extension methods should be created with naming schemes that avoid that possibility as much as possible. Try for instance using less common verbs for what you are doing. So AquireData rather then GetData, that way you get the definition you want, and the peace of mind that there is a low chance that the underlying developers would likely choose the simpler word over the more &amp;quot;complex&amp;quot; one.&lt;/p&gt;
&lt;p&gt;Warning on extension method hiding would be nice, although I suspect that it will be coming in a future version.&lt;/p&gt;
&lt;p&gt;About your #3 P.S., IEnumerable&amp;lt;T&amp;gt; is an enumerable of objects, it just also happens to be an enumerable of T's, not having it inherit from IEnumerable would not make logical sense, shouldn't an enumerable of T be able to treated as an enumerable of objects?&lt;/p&gt;
&lt;p&gt;Javier,&lt;/p&gt;
&lt;p&gt;They have similar issues, but method hiding requires active work to get to be extremely complex (After all it is designed to be used sparingly), compared to polymorphism's ability to quickly mess up everything if one of your parent classes adds a new method that matches the other parent.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8671954" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#8635086</link><pubDate>Sun, 22 Jun 2008 03:26:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8635086</guid><dc:creator>Javier</dc:creator><description>&lt;p&gt;How is method hiding different from polimorphism?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8635086" width="1" height="1"&gt;</description></item><item><title>re: Method Hiding Apologia</title><link>http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx#8566816</link><pubDate>Sat, 31 May 2008 19:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8566816</guid><dc:creator>Konstantin Triger</dc:creator><description>&lt;p&gt;I think the method hiding feature is very useful, yet I think it should not get into the language.&lt;/p&gt;
&lt;p&gt;The reason is because it's a _client_ feature. In other words, if the client is already compiled, replacing the server will not affect the client behavior...&lt;/p&gt;
&lt;p&gt;In this view 'method hiding' is very similar to 'default parameters' feature, which, as far I know, was not included into the language because of the very same reason.&lt;/p&gt;
&lt;p&gt;So what is the solution? In my opinion it's extension methods, implemented by the _client_, while the server make all its methods unambiguous.&lt;/p&gt;
&lt;p&gt;Thus my operational proposition is to make extension methods _precede_ class methods in resolution chain, letting the above suggestion possible.&lt;/p&gt;
&lt;p&gt;P.S.&lt;/p&gt;
&lt;p&gt;1. The fact that if I have an extension method ambiguous with the class method, it simply disappears (no warning or similar) looks buggy (csc 3.5).&lt;/p&gt;
&lt;p&gt;2. Having a possibility to implement the same interface several times along the class derivation chain actually is an instance of the same 'feature'. Since it's used a lot in .Net BCL, as mono contributor I several times fixed bugs caused by different implementation of the same interface along the chain.&lt;/p&gt;
&lt;p&gt;3. Q: what about IEnumerable&amp;lt;T&amp;gt;, it does not fit with the guidelines. A: To my opinion IEnumerable&amp;lt;T&amp;gt; shouldn't derive from IEnumerable. In general, derivation for hiding looks problematic to me due to very hard runtime bugs, which it enables.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8566816" width="1" height="1"&gt;</description></item></channel></rss>