Does this keyword exist? When an overriding method needs to call the parent

前端 未结 3 1215
说谎
说谎 2021-01-12 20:46

Often, in C# documentation, you come across a member where the description says something along the lines of \"be sure to call the base method if you override this.\"

<
3条回答
  •  野的像风
    2021-01-12 21:13

    You can call it like this:

    public override void MyFunction()
    {
        // do dome stuff
        SomeStuff();
    
       // call the base implementation
       base.MyFunction();
    }
    

    But if you're asking whether it can be "checked" at compile time - no.

    You could probably use a dependency analysis tool such as NDepend to create rules to check this is being done, and have the rules run as part of the compile, but I don't think you can do the enforecement from the compiler itself.

提交回复
热议问题