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

前端 未结 6 1046
春和景丽
春和景丽 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:28

    This might help.

    class Base
    {
       public void F() {}
    }
    class Derived: Base
    {
       public void F() {}      // Warning, hiding an inherited name
    }
    

    In the above example, the declaration of F in Derived causes a warning to be reported. Hiding an inherited name is specifically not an error, since that would preclude separate evolution of base classes. For example, the above situation might have come about because a later version of Base introduced an F method that wasn't present in an earlier version of the class. Had the above situation been an error, then any change made to a base class in a separately versioned class library could potentially cause derived classes to become invalid. Source: https://msdn.microsoft.com/en-us/library/aa691135%28v=vs.71%29.aspx

提交回复
热议问题