WHy should virtual methods be explicitly overridden in C#?

前端 未结 5 1696
遇见更好的自我
遇见更好的自我 2021-02-20 02:08

Why should virtual methods be explicitly overridden in C#?

5条回答
  •  攒了一身酷
    2021-02-20 02:36

    Not all virtual methods should be overridden, though all abstract methods should (and must) be. As for why the 'override' keyword is explicit, that's because overriding and hiding behave differently. A hiding method is not called through a reference to a base class, whereas an overridden method is. This is why the compiler specifically warns about how you should use the 'new' keyword in the case where you are hiding rather than overriding.

提交回复
热议问题