开发者

hide a baseclass public method with new modifier

I have class myCollection that inherit from Dictionary. Want to hide the add method in myCollection. Used the p开发者_Python百科rivate new modifier but its still visible. Is this not possible ?

baseclass

    public void Add(TKey key, TValue value)
    {
        Insert(key, value, true);
    }

mycollection

    private new void Add(string key, MyOtherClass myClass)
    {
        base.Add(key, myClass);    
    }


No, this is not possible.

You can't change the accessibility of an inherited member.

You can use composition instead of inheritance and only expose the functionality you want (by delegating calls to the composed object). This may not be an option if you rely on the inheritance chain elsewhere in your code.


It's not hidden because the method signature of the Add in baseclass doesn't match that of the method in the mycollection class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜