C# and method hiding

后端 未结 9 1184
悲&欢浪女
悲&欢浪女 2021-01-03 07:48

Per MSDN, the \"new\" keyword when used for method hiding only suppresses a warning.

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

Do I really need

相关标签:
9条回答
  • 2021-01-03 08:18

    Method hiding is not the same as overriding. Do not use it where overriding should be your choice as they work differently.

    I wrote about this some time ago, you might want to read Method hiding or overriding - or the difference between new and virtual for more details.

    Method hiding should be discouraged because it changes the semantics of a class. The functionality changes depending on if you have a reference to the base, or the actual class. That's the main reason there is a compiler warning about it. It can do unexpected things if the caller is not aware that method hiding is in use.

    ADDITIONAL

    Based on updates to the question. Method Hiding does not require the keyword new, although you should make it explicit and use the keyword if that is what you are doing. It removes the compiler warning and alerts other developers (or yourself 6 months down the line) what your intention was.

    I still wouldn't recommend method hiding, tho'

    UPDATE: 14/5/11

    I moved my blog, so I'm updating any old links

    0 讨论(0)
  • 2021-01-03 08:19

    The use of new is when you want a function of the same name, but you don't want to override. It breaks the virtual dispatch chain.

    This might happen when somebody adds a function to a base class that has the same name as your derived class function.

    0 讨论(0)
  • 2021-01-03 08:21

    The only difference between using "new" and "not using new" is that, you will get a compiler warning when you don't use the "new" keyword, to alert the developer of any inadvertent method hiding that is happening. At the following URL you can also find an excellent video explaining method hiding and the difference between using "new" and "not using new".

    0 讨论(0)
  • 2021-01-03 08:23

    Thanks guys. I am aware of the differences between hiding and overriding, I was just curious if there was a functionality difference between using "new" and not using "new" for hiding. According to all of your answers, it seems not although as someone had raised a good point in that it makes for cleaner code and it may be necessary in the future. THanks again Folks!

    0 讨论(0)
  • 2021-01-03 08:24

    It's part of C#, just like ref and out. The idea is to warn you that your API is overriding a method which could affect code that relies on sub/superclasses of your class.

    Placing the new keyword forces you to think about whether that is really what you intended to do or not, just like requiring ref or out so that you have to think about the contract your code is providing.

    0 讨论(0)
  • 2021-01-03 08:26

    The new keyword should always be used when hiding (shadowing) methods. Although technically it does not make a difference to functionality, as you correctly point out, it is strongly recommended.

    The prescence of the keyword not only indicates clearly to the reader of the code that you are explicitly hiding a method of the same name in the base class, but also its usage is part of the official (MSDN) C# guidelines, quite possibly because its usage may be required in the future.

    The reason that the current compiler only gives you a warning (rather than an error) when you omit the keyword is purely for reasons of backward compatibility. C# 1.0 does not support the new keyword for hiding members of classes, as MSDN suggests, and method (generally member) hiding was performed then automatically. I suspect MS will try to maintain backward compatibility in this respect, but it's certainly not guaranteed in future versions of C#.

    0 讨论(0)
提交回复
热议问题