0% found this document useful (0 votes)
213 views42 pages

Method Hiding Apologia - Fabulous Adventures in Coding

It talks about method hiding in programming.

Uploaded by

Pradeep Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views42 pages

Method Hiding Apologia - Fabulous Adventures in Coding

It talks about method hiding in programming.

Uploaded by

Pradeep Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.

Learn more (https://go.microsoft.com/fwlink/?linkid=845480)

(http://msdn.microsoft.com/)| Developer

Method Hiding Apologia


Rate this article ★
★★★★
★★
★★★★★
★★★

Eric Lippert (https://social.msdn.microsoft.com/profile/Eric+Lippert) May 21, 2008

 25 (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-
apologia/#comments)
Share 0 0 0

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 1/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 2/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 3/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 4/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 5/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 6/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 7/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 8/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 9/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 10/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 11/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 12/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 13/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 14/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 15/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 16/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 17/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 18/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 19/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 20/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 21/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 22/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 23/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 24/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 25/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 26/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 27/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

Tags C# (https://blogs.msdn.microsoft.com/ericlippert/tag/c/) Dialogue


(https://blogs.msdn.microsoft.com/ericlippert/tag/dialogue/) Software development methodology
(https://blogs.msdn.microsoft.com/ericlippert/tag/software-development-methodology/)

Comments (25)

Jeff Parker
May 21, 2008 at 1:56 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5008)
I dunno for me this method hiding just gets confusing and starts to be spaghetti like.

For example using the following code you can see the method is not really hidden I mean it
makes sense why it isn’t but if you were to blow this out on a few classes that inherit from each
other then you can really make quite a confusing mess of things.

  class Program

   {

       static void Main(string[] args)

       {

           A a = new A();

           B b = new B();

           C c = new C();

           D d = new D();

           a.Foo();

           b.Foo();

           c.Foo();

           d.Foo();

           a = new B();

           a.Foo();

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 28/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

           a = new C();

           a.Foo();

           a = new D();

           a.Foo();

       }

   }

   class A

   {

       public void Foo() {Console.WriteLine("A Foo");}

   }

   class B : A

   {

       public new void Foo() { Console.WriteLine("B Foo"); }

   }

   class C : B

   {

       public new void Foo() { Console.WriteLine("C Foo"); }

   }

   class D : C

   {

       public new void Foo() { Console.WriteLine("D Foo"); }

   }

Running that gives you

A Foo

B Foo

C Foo

D Foo

A Foo

A Foo

A Foo

It also doesn’t seem to be able to go in reverse like a D can be an A. To me this is just not… I don;t
know the word I am looking for. maybe clean, kind of confusing, I mean I know what I did
because I wrote al the classes but you give someone just D and they do not know about B or C
and well just could lead to many problems.
https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 29/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

Anders Borum (http://www.sphereworks.dk)


May 21, 2008 at 2:17 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5010)
Hi Eric,

As I see it, method hiding is the approach developers take when they can’t do the "return type
covariance" dance. It’s nice to able to hide methods and as such I think it’s definately important to
C#, but unless the developer careful about the implementation, nasty side effects may show up.

Calling the hidden method on a base type reveals the underlying implementation. You might say
that this is a desired design, but currently this is not an intuitive or natural approach to designing
an API I think.

C# needs return type covariance (the following C# implemented for illustrative purposes).
Looking forward to this feature in C# 4.0 (or C# 3.0 SP1).

// Lackof return type covariance

class A {

  public A Parent { get { return this.parent; }

class B : A {

  public new B Parent { get { return base.A as B; } }

// Return type covariance plays the nice game

class A {

  public virtual A Parent { get { return this.parent; }

class B : A {

  override public B Parent { get { return base.A as B; } }

Daniel Grunwald
May 21, 2008 at 2:20 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5011)
Another place where method hiding is important is versioning.

If a base class adds a new method (and maybe calls it internally); method hiding allows code
compiled against the old version of the class to still run without unintended consequences. It also
allows the code to be recompiled without changing behavior – you get a compiler warning
because you’re hiding without the "new" keyword, but your program still works if you choose to
ignore the warning.

Eric Lippert (https://social.msdn.microsoft.com/profile/Eric+Lippert)


https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 30/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

May 21, 2008 at 2:22 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-


hiding-apologia/#comment-5012)
> C# needs return type covariance

Needs?  It’s managed to become a successful, useful language without return type covariance for
the last three releases over almost a decade.

> Looking forward to this feature in C# 4.0 (or C# 3.0 SP1).

I’m afraid that you’re going to be disappointed.

Anders Borum (http://www.sphereworks.dk)


May 21, 2008 at 2:46 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5013)
Eric,

Well, I’m perhaps a bit biased!

Don’t get me wrong; C# is the best thing since sliced bread, but stating the language has been
succesful without return type covariance for three releases doesn’t mean it’s an un-important
feature. You’re asking for comments, and return type covariance definitely relates to method
hiding.

Eric Lippert (https://social.msdn.microsoft.com/profile/Eric+Lippert)


May 21, 2008 at 2:54 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5014)
Well, don’t get me wrong — I would find return type covariance useful as well. That doesn’t mean
that its ever going to happen.

We have no shortage of features — we made a list of several dozens of possible features for
future versions, and the classified them into "bad idea", "not worth doing", "useful", "great", and
"must have for the next version".  

Then we cut all but the last category.  Then we cut about half of that.  Return type covariance did
not make that first cut, much less the second.

Merely being useful is nowhere _near_ to our bar for actually designing, specifying, implementing,
testing, documenting and maintaining a feature.

Anders Borum (http://www.sphereworks.dk)


May 21, 2008 at 3:13 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5015)
Eric,

Thanks for the sharing some insights on your teams decisions. It’s this kind of information that
makes it easier to uderstand the design decisions that went into the language. I spend many
hours with C# every day and have been looking for an educated answer to why return type
covariance was left out of the specs. Now, I don’t have extensive experience in the field of
language design, but it just "felt" like a "useful" feature that didn’t "seem" to have any
unintentional side effects on the current specifications.

I stated that this feature in particular is "useful", but I might have used a different adjective.
Regardless, consider this a vote on the feature.
https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 31/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

Eric Lippert (https://social.msdn.microsoft.com/profile/Eric+Lippert)


May 21, 2008 at 4:05 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5016)
Indeed, lots of people want this feature. C++ has it. I can see how it would be handy.

A point against it is the fact that the CLR does not natively support it; in order to make it work,
the C# compiler would need to… you guessed it. Generate a hiding method with a different
signature and then having the virtual method call it. Essentially, all the compiler would be doing is
exactly what you now have to do in order to get something that looks like that, just hiding the
indirection and the cost from you.

That’s not in itself a bad thing — lots of "high level" features are things that you could plumb
together yourself — anonymous functions, iterators, "using" blocks, etc.  But it does illustrate that
this would be solely a syntactic sugar, not really much of a fundamentally new feature.

configurator (http://blog.tapuchim.com)
May 22, 2008 at 1:58 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5017)
Since we don’t have return type covariance, we use method hiding to change the return type.
Here’s a rather silly example:

public class TimeKeeper {

public virtual string WhatTimeIsIt() {

return DateTime.Now.ToShortTimeString();

public class UniversalTimeKeeper : TimeKeeper {

public new DateTime WhatTimeIsIt() {

return DateTime.Now.ToUniversalTime();

Note: the derived class does two things here: it changes both the return type AND the value.

Now we have a problem: suppose someone used a

TimeKeeper value = new UniversalTimeKeeper();

string time = value.WhatTimeIsIt();

we would want him to get the universal time, since this is a UniversalTimeKeeper after all and
that’s what virtual methods are for.

But we did not override the function!

If there is no return type covariance, there could at least be a syntax to do both an override and a
hide:

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 32/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

public class UniversalTimeKeeper : TimeKeeper {

public override string WhatTimeIsIt() {

return DataTime.Now.ToUniversalTime().ToShortTimeString();

public new DateTime WhatTimeIsIt() {

return DataTime.Now.ToUniversalTime();

TimeKeeper t1 = new UniversalTimeKeeper();

UniversalTimeKeeper t2 = new UniversalTimeKeeper();

t1.WhatTimeIsIt(); // calls overriden method

t2.WhatTimeIsIt(); // calls new method

I know. This is a little awkuard and potentially confusing. Does anyone have a better idea?

Frederik Siekmann
May 22, 2008 at 2:14 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5018)
@Anders:

I have somehow ‘solved’ your problem with covariance a few times using generics. A little more
precisely I introduced an generic parameter describing the type of the derived class.

In your example:

class A<TSelf>

where TSelf : A<TSelf>

 public TSelf Parent { get { return this.parent; } }

and

class B : A<B>

now B has somehow magically get a parent of Type B.

Although I used this technique a few times, I still don’t know whether this is actually a good thing
to do or a clear abuse of generics. Anyone got an opinion about that?

Weeble

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 33/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

May 22, 2008 at 9:42 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-


hiding-apologia/#comment-5019)
I was surprised that this doesn’t work:

public class Base

public virtual Base MakeAnother() { return new Base(); }

public class Derived : Base

override Base Base.MakeAnother() { return this.MakeAnother(); }

public new Derived MakeAnother() { return new Derived(); }

As configurator suggests, I’m trying to do an override and a hide. I thought you might be able to
do an "explicit" override, in the same way you can explicitly implement an interface. From what
Eric says ("generate a hiding method with a different signature and then having the virtual
method call it") I guess that you have to make some arbitrary change to the parameter list of the
new MakeAnother() introduced by Derived, which seems worse than coming up with a new name
for the method.

Charlie Calvert's Community Blog (http://blogs.msdn.com/charlie/archive/2008/05/22/vcs-team-


links-for-may-22-2008.aspx)
May 22, 2008 at 3:04 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5020)
Rather than place the links to the most recent C# team content directly in Community
Convergence, I have

Jeff A
May 23, 2008 at 10:14 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5032)
Frederick,

The main problem with your proposal is that is an evolutionary dead end with respect to
covariance.  That is to say this approach only allows a one level inheritance heirarchy that
supports covariance.  To achieve covariance again on a class C: B you would have to again engage
in method hiding.

It also isn’t enforcing what you think it might be.  For example consider:

class D: A<D> { }

class E: A<D> { }

You probably didn’t want class E to be allowable.  But that is just a matter of disciplined coding by
the consumer of class A.

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 34/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

I must however admit I have often desired a built-in psuedo-type called Self for use with
covariance.

Frederik Siekmann
May 23, 2008 at 3:51 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5039)
Jeff,

I don’t know for sure if that’s the point you made, but you can allow multiple levels of hierarchy:

public abstract class A<TSelf> {…}

public abstract class B<TSelf> : A<TSelf> {…}

public class B : B<B> {…}

public class C : B<C>

and so on.

Or did you meant the problem that you can cast an B<B> to A<B> but not to A<A> (or in the
above example C to B)?

Jeff A
May 24, 2008 at 11:25 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5040)
Frederick,

Yes, you could extend the inheritance heirarchy in that fashion but it would require that you
declare the abstract version of each class you wanted to be extensible.  This seems much the
same amount of work as just using method hiding in the first place (I suppose if you had lots of
covariant methods this would be a convenient time saver).

Yes, not being able to cast C to B is one problem.  You could get around that by making all
methods that take A or B as parameters generic (i.e. public void DoSomething<Self>(B<Self>))
but that also seems like a lot of work to maintain polymorphic behaviour.

The other problem I was referring to was lets say you had:

class Animal<Self> where Self: Animal, new()

public Self Reproduce() { return new Self(); }

class Cat: Animal<Cat> { }

class Dog: Animal<Cat> { }

Now we have a dog that gives birth to a cat when it reproduces!! I don’t think this is the type of
covariance you wanted to enable.

Konstantin Triger (http://ktriger.blogspot.com/)

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 35/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

May 31, 2008 at 12:13 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-


hiding-apologia/#comment-5054)
I think the method hiding feature is very useful, yet I think it should not get into the language.

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…

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.

So what is the solution? In my opinion it’s extension methods, implemented by the _client_, while
the server make all its methods unambiguous.

Thus my operational proposition is to make extension methods _precede_ class methods in


resolution chain, letting the above suggestion possible.

P.S.

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).

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.

3. Q: what about IEnumerable<T>, it does not fit with the guidelines. A: To my opinion
IEnumerable<T> shouldn’t derive from IEnumerable. In general, derivation for hiding looks
problematic to me due to very hard runtime bugs, which it enables.

Javier
June 21, 2008 at 8:26 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5101)
How is method hiding different from polimorphism?

Joshua
June 30, 2008 at 1:37 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5137)
Konstantin Triger,

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)

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
"complex" one.

Warning on extension method hiding would be nice, although I suspect that it will be coming in a
future version.

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 36/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

About your #3 P.S., IEnumerable<T> 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?

Javier,

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.

Konstantin Triger
July 1, 2008 at 3:23 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5141)
To Joshua:

Thanks for your comments!

I think all this thread is a "post mortem" 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.

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.:

class Y {

   public void test() {

       Console.WriteLine("Y");

   }

class X : Y {

   public void testEx() {

       Console.WriteLine("X");

   }

//declared by the user in  the user code!

static class ExtensionX {

   public static void test(this X x)  {

       x.testEx();

   }

new X().test(); // -> ‘X’ is printed (currently the output is ‘Y’)

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 37/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

So the user will be able to call a ‘simpler’ extension method.

About my and your #3 P.S.: Following your logic IEnumerable<T> should be also
IEnumerable<object>, which is not the case. If due to some reason you need to convert your
IEnumerable<T> to IEnumerable, one way would be a method like this:

public static IEnumerable ToGenericIEnumerable<T>(IEnumerable<T> e) {

   foreach (T t in e)

       yield return t;

Of course that’s not needed, since IEnumerable<T> derives from IEnumerable, but what would
you do if you needed to convert IEnumerable<T> to IEnumerable<X>, provided T derives from X?

Alex Grenier
July 3, 2008 at 3:28 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5145)
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 "downgrade specialization". 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.

David Nelson (http://www.commongenius.com)


July 25, 2008 at 5:29 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5174)
@Konstantin,

The reason IEnumerable<T> needs to implement IEnumerable but doesn’t need to worry about
implementing IEnumerable<object> is so that a reference to IEnumerable<T> can be used with
code that pre-dates generics. For example:

public void OldFoo(IEnumerable bars)

// …

public void NewFoo<T>(IEnumerable<T> bars)

  // This doesn’t work if IEnumerable<T> doesn’t implement IEnumerable

  OldFoo(bars);

While you could say the same thing about IEnumerable<object>, there are well known ways
around that problem (using generic methods instead of IEnumerable<object>, for example),
whereas with pre-generic code the only workaround is writing a wrapper class. Also, generic

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 38/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

covariance, if it ever gets added to the language, will take care of the IEnumerable<object>
problem, whereas it can’t take care of the IEnumerable problem since they are two different types.

Nick
November 5, 2008 at 4:06 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5528)
Personally, programming is not about creating frankenstein concepts. This cake example in my
view demonstrates exactly how not to use these concepts.  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.  Programming is hard enough right?

Brian Russo (http://entropy.net)


February 19, 2009 at 2:37 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-5891)
I think this is a fairly decent article.. but frankly I’m not sold on method hiding at all.

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.

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.

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.

cheers,

– bri

Ralph Compton
August 3, 2009 at 10:59 am (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-7311)
I still don’t see why the language includes both method overriding and hiding. Can this be
explained without a reference to covariance?

Jeremy S. Pederson
March 9, 2011 at 10:37 pm (https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-
hiding-apologia/#comment-11285)
I personally like this feature… backwards compatibility

//old communication object

internal class CommunicationObject : ICommunicationObject

   public CommunicationObject(ISomeInterface someInterface)

   {

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 39/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

        this.ISomeInterface = someInterface;

   }

   protected ISomeInterface { get; private set; }

//new, more advanced, communication object

internal class CommunicationObjectEx : CommunicationObject, ICommunicationObjectEx

   public CommunicationObjectEx(ISomeInterfaceEx someInterface)

       : base(someInterface) {   }

   new protected ISomeInterfaceEx SomeInterface

   {

        get { return base.SomeInterface as ISomeInterfaceEx; }

   }

//public facing factory

public static CommunicationObjectFactory

   //old factory method

   public static ICommunicationObject CreateCommunicationObject()

   {

       return new CommunicationObjectEx(new SomeClassEx());

   }

   //new factory method that lets the caller initialize a new version of the communication object
without breaking old code

   public static ICommunicationObjectEx CreateCommunicationObjectEx()

   {

       return new CommunicationObjectEx(new SomeClassEx());

   }

Comments are closed.


Follow Us


https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 40/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

 (https://blogs.msdn.microsoft.com/ericlippert/feed/)

Popular Tags
C# (https://blogs.msdn.microsoft.com/ericlippert/tag/c/)

Scripting (https://blogs.msdn.microsoft.com/ericlippert/tag/scripting/)

JScript (https://blogs.msdn.microsoft.com/ericlippert/tag/jscript/)

VBScript (https://blogs.msdn.microsoft.com/ericlippert/tag/vbscript/)

Language Design (https://blogs.msdn.microsoft.com/ericlippert/tag/language-design/)

COM Programming (https://blogs.msdn.microsoft.com/ericlippert/tag/com-programming/)

Rarefied Heights (https://blogs.msdn.microsoft.com/ericlippert/tag/rarefied-heights/)

Puzzles (https://blogs.msdn.microsoft.com/ericlippert/tag/puzzles/)

Rants (https://blogs.msdn.microsoft.com/ericlippert/tag/rants/)

Performance (https://blogs.msdn.microsoft.com/ericlippert/tag/performance/)

Security (https://blogs.msdn.microsoft.com/ericlippert/tag/security/)

C# 4.0 (https://blogs.msdn.microsoft.com/ericlippert/tag/c-4-0/)

Non-computer (https://blogs.msdn.microsoft.com/ericlippert/tag/non-computer/)

SimpleScript (https://blogs.msdn.microsoft.com/ericlippert/tag/simplescript/)

JScript .NET (https://blogs.msdn.microsoft.com/ericlippert/tag/jscript-net/)

Immutability (https://blogs.msdn.microsoft.com/ericlippert/tag/immutability/)

Code Quality (https://blogs.msdn.microsoft.com/ericlippert/tag/code-quality/)

Pages (https://blogs.msdn.microsoft.com/ericlippert/tag/pages/)

Recursion (https://blogs.msdn.microsoft.com/ericlippert/tag/recursion/)

Books (https://blogs.msdn.microsoft.com/ericlippert/tag/books/)

Archives
November 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/11/) (5)
October 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/10/) (5)
September 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/09/) (2)
August 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/08/) (5)
July 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/07/) (2)
June 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/06/) (6)
May 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/05/) (2)
April 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/04/) (5)
March 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/03/) (3)

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 41/42
11/22/2019 Method Hiding Apologia – Fabulous Adventures In Coding

February 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/02/) (7)


January 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/01/) (5)
All of 2012 (https://blogs.msdn.microsoft.com/ericlippert/2012/) (47)
All of 2011 (https://blogs.msdn.microsoft.com/ericlippert/2011/) (66)
All of 2010 (https://blogs.msdn.microsoft.com/ericlippert/2010/) (90)
All of 2009 (https://blogs.msdn.microsoft.com/ericlippert/2009/) (100)
All of 2008 (https://blogs.msdn.microsoft.com/ericlippert/2008/) (43)
All of 2007 (https://blogs.msdn.microsoft.com/ericlippert/2007/) (63)
All of 2006 (https://blogs.msdn.microsoft.com/ericlippert/2006/) (25)
All of 2005 (https://blogs.msdn.microsoft.com/ericlippert/2005/) (97)
All of 2004 (https://blogs.msdn.microsoft.com/ericlippert/2004/) (167)
All of 2003 (https://blogs.msdn.microsoft.com/ericlippert/2003/) (88)

Privacy (https://privacy.microsoft.com) Terms of Use (https://msdn.microsoft.com/cc300389)


Trademarks (https://www.microsoft.com/en-us/legal/intellectualproperty/Trademarks/EN-US.aspx)
(https://www.microsoft.com)
© 2019 Microsoft

https://blogs.msdn.microsoft.com/ericlippert/2008/05/21/method-hiding-apologia/ 42/42

You might also like