C# static member “inheritance” - why does this exist at all?

前端 未结 7 1399
忘掉有多难
忘掉有多难 2020-11-27 19:32

In C#, a superclass\'s static members are \"inherited\" into the subclasses scope. For instance:

class A { public static int M() { return 1; } }
class B : A         


        
7条回答
  •  庸人自扰
    2020-11-27 20:01

    Actually, as I understand it, this is just a shortcut provided by the compiler. Syntax sugar. B.M() will just compile to A.M() since B does not have a static M() and A does. It's for easier writing, nothing else. There is no "static inheritance".

    Added: And the requirement for new when "redefining" is just so that you don't accidentally shoot yourself in the foot.

提交回复
热议问题