C# and method hiding -
The "new" keyword used per MSDN, the "hidden" method, simply suppresses a warning
To hide the method, do I really need a "new" keyword? If I have a child who has the same name of the same method but clearly does not make it clear that it is an override, then this is not necessarily the same thing, I just got a warning? Please tell me that my understanding is correct. Thank you
You hide the method whether you specify "new" or not, but it's overriding not quite the same . Here's an example where they are different:
using the system; Class Base {Public Virtual Zero Override Me () {Console.WriteLine ("Base.OverrideMe"); } Public Virtual Zero HideMe () {Console.WriteLine ("Base.HideMe"); }} Category derived: base {public override zero override () {Console.WriteLine ("Derive.OverrideMe"); } Public New Zero HideMe () {Console.WriteLine ("Derived.HideMe"); }} Class Test {Static Zero Main () {Base X = New Deferred (); X.OverrideMe (); X.HideMe (); }}
Output is:
generated. Override base. While the base HideMe
method is virtual, it has not been overridden in derived
, it is currently hidden - so the method Still in the base
, the virtual method is bound, and that is what is executed. The member is usually a bad idea, which makes it difficult to understand the code, the fact is that it is beneficial in terms of versioning available , though - it means That adding a method to the base class does not inadvertently override the derivative orbits, and they can work as before. That's why you get warnings.
Comments
Post a Comment