method hiding in c# with a valid example. why is it implemented in the framework? what is the Real world advantage?

前端 未结 6 1060
春和景丽
春和景丽 2020-12-05 19:53

Can anyone explain the actual use of method hiding in C# with a valid example ?

If the method is defined using the new keyword in the d

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 20:34

    One time when you would use them is when you need to add/modify an attribute that is on a base-class method. For example:

    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    public new event EventHandler TextChanged
    {
        add { base.TextChanged += value; }
        remove { base.TextChanged -= value; }
    }
    

    The base Control class has a TextChanged event, but the base event has all kinds of attributes slapped on it to prevent it from showing up in intellisense or the designer. Because the class I used that in makes extensive use of both the Text property and the TextChanged event, I wanted the TextChanged event to show up in intellisense and be visible in the properties window.

提交回复
热议问题