What is the difference between the override and new keywords in C#?

后端 未结 5 1172
无人共我
无人共我 2020-11-28 21:49

What is the difference between the override and new keywords in C# when defining methods in class hierarchies?

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 22:29

    The following page summarizes your question very nicely.

    Knowing When to Use Override and New Keywords

    Summary

    Override: When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class.

    New: If you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it.

    If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).

    Override: used with virtual/abstract/override type of method in base class

    New: when base class has not declared method as virtual/abstract/override

提交回复
热议问题