Use new keyword if hiding was intended

后端 未结 4 1012
日久生厌
日久生厌 2020-12-13 23:11

I have the following snippet of code that\'s generating the \"Use new keyword if hiding was intended\" warning in VS2008:

public double Foo(double param)
{
          


        
4条回答
  •  猫巷女王i
    2020-12-14 00:00

    The key is that you're not overriding the method. You're hiding it. If you were overriding it, you'd need the override keyword (at which point, unless it's virtual, the compiler would complain because you can't override a non-virtual method).

    You use the new keyword to tell both the compiler and anyone reading the code, "It's okay, I know this is only hiding the base method and not overriding it - that's what I meant to do."

    Frankly I think it's rarely a good idea to hide methods - I'd use a different method name, like Craig suggested - but that's a different discussion.

提交回复
热议问题