WHy should virtual methods be explicitly overridden in C#?

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

Why should virtual methods be explicitly overridden in C#?

5条回答
  •  时光取名叫无心
    2021-02-20 02:15

    Because it makes code more readable:

    class Derived : Base
    {
        void Foo();
    }
    

    In C++, Foo may or may not be a virtual method, we can't tell by looking at the definition. In C# we know a method is virtual (or isn't) because there is either a virtual or override keyword.

    Jason's comment below is a better answer.

    (edited for clarity)

提交回复
热议问题