C # and hiding method

In MSDN, the keyword "new" when used to hide a method only suppresses the warning.

http://msdn.microsoft.com/en-us/library/435f1dw2.aspx

Do I really need this "new" keyword to perform method hiding? If I have a child class that has the same method name, but does not explicitly indicate that it overrides, is this not the same, I just get a warning? Please tell me if I understand correctly. Thanks

+3
source share
9 answers

You get a method that hides whether you specify "new", but this is not the same as overriding. Here is an example where they are different:

using System; class Base { public virtual void OverrideMe() { Console.WriteLine("Base.OverrideMe"); } public virtual void HideMe() { Console.WriteLine("Base.HideMe"); } } class Derived : Base { public override void OverrideMe() { Console.WriteLine("Derived.OverrideMe"); } public new void HideMe() { Console.WriteLine("Derived.HideMe"); } } class Test { static void Main() { Base x = new Derived(); x.OverrideMe(); x.HideMe(); } } 

Output:

 Derived.OverrideMe Base.HideMe 

Despite the fact that the underlying HideMe method is virtual, it is not overridden in Derived , it is simply hidden - therefore, the method is still bound to the virtual method in Base , and what is being executed.

Hiding members is usually a bad idea, which makes code more difficult to understand. However, the fact that it is available is useful from the point of view of version control - this means that adding a method to the base class does not allow potential classes to override it unintentionally, and they can continue to work as before. That is why you get a warning.

+9
source

Hiding a method is not the same as overriding. Do not use it when overriding should be your choice as they work differently.

I wrote about this a while ago, you can read the Method that hides or overrides, or the difference between new and virtual for more details.

Hiding the method should be discouraged as it changes the semantics of the class. Functionality varies depending on whether there is a link to the base or the actual class. This is the main reason this is a compiler warning. He can do unexpected things if the caller does not know that the hide method is being used.

ADDITIONAL

Based on question updates. The Hiding method does not require the new keyword, although you must make it explicit and use the keyword if that is what you are doing. It removes the compiler warning and warns other developers (or you after 6 months) that you did.

I still don't recommend hiding the method, tho '

UPDATE: 14/5/11

I moved my blog, so I am updating any old links

+1
source

The new keyword should always be used when hiding methods. Although technically this does not affect functionality, as you correctly point out, it is highly recommended.

The presence of the keyword not only clearly indicates to the code reader that you explicitly hide the method with the same name in the base class, but also its use is part of the official (# MSDN) recommendations of C #, it is possible because its use may be required in the future.

The reason the current compiler gives you a warning (rather than an error) when omitting a keyword is only for backward compatibility reasons. C # 1.0 does not support the new keyword to hide class members, as MSDN suggests, and then the method (usually the member) is hidden automatically. I suspect that MS will try to maintain backward compatibility in this regard, but this is certainly not guaranteed in future versions of C #.

+1
source

Using a new keyword when hiding a method is to "make the programmer's intention understandable." This avoids accidental programmer exits.

If the new keyword is missing, the compiler issues a warning and treats it as if it were present.

You can learn more about this using Versions with overrides and new keywords (C # Programming Guide)

+1
source

The only difference between using the “new” and “not using the new” is that you will get a compiler warning when you do not use the “new” keyword to warn the developer of any inadvertent hiding of the method that happens. In the following URL You can also find a great video address explaining the method of hiding and the difference between using the “new” and “not using the new” .

+1
source

This is part of C #, just like ref and out. The idea is to warn you that your API overrides a method that can affect code that depends on the sub / superclasses of your class.

Placing a new keyword makes you wonder if this is really what you intended to do or not, as well as requiring ref or out so you can think about the contract that your code provides.

0
source

As far as I know, the only difference is what you suggest: new hides the warning. I tried the following code:

 class Base { public void Test(){} } class ChildA : Base { public void Test(){} } class ChildB : Base { public new void Test(){} } 

The testing method for both classes looks identical in IL:

 .method public hidebysig instance void Test() cil managed { // Code size 1 (0x1) .maxstack 8 IL_0000: ret } // end of method ChildA::Test .method public hidebysig instance void Test() cil managed { // Code size 1 (0x1) .maxstack 8 IL_0000: ret } // end of method ChildB::Test 

The compiler issued a warning for Test in ClassA , but not in ClassB . But as the other answers said; do not confuse the concept of method hiding and method overrides; Not the same.

0
source

Using the new is when you need a function with the same name, but you don't want to override it. It destroys the virtual dispatch chain.

This can happen if someone adds a function to a base class that has the same name as your derived class function.

0
source

Thanks guys. I know the differences between hiding and overriding, I was just curious if there is a difference in functionality between using the “new” and not using the “new” to hide. According to all of your answers, it seems that although someone raised a good point that he is making cleaner code, and this may be needed in the future. Damn people again!

0
source

All Articles