Roy Osherove

View Original

Missing C# feature ? - Totally hiding derived members

So here's my problem. I have class Foo:

public class Foo{

public void DoStuff()

{

...

}

}

and i have class Bar derived from it. I want class Bar to Hide the "DoStuff" and expose it's own implementation of "DoStuff()". Simple, right?

public class Bar:Foo

{

//Hide the default memebr

new private void DoStuff(){

}

public void DoStuff(int argument){}

}

Ok. Done. But watch this: writing the following line in VS.NET:

Bar b = new Bar();

b.    ///->>>>You get both the Bar.DoStuff AND the Foo.DoStuff in the Tooltips.

meaning : you cannot hide inharited members totally. you must implement them to hide the default functionality. I looked it up on MSDN and found this article which states so clearly:

"A declaration of a new member hides an inherited member only within the scope of the new member.

class Base { public static void F() {} } class Derived: Base { new private static void F() {} // Hides Base.F in Derived only } class MoreDerived: Derived { static void G() { F(); } // Invokes Base.F }

In the example above, the declaration of F in Derived hides the F that was inherited from Base, but since the new F in Derived has private access, its scope does not extend to MoreDerived. Thus, the call F() in MoreDerived.G is valid and will invoke Base.F."

How is that not possible? you'd think it's a pretty legitimate programming way, wouldn;t you? I can;t think of any situation where having this as a possibility is a bad thing...