Search code examples
c#oopinheritancevirtualprotected

this. vs base. for inherited protected non-virtual methods?


Whithin my sub-class, should I refer to an inherited protected non-virtual method as this.Method() or base.Method()?

Using this would allow me to easily hide the method with a new method of the same name. Should calls to the method explicitly specifiy base only when it is certain that only the base class' implementation specifically needs to be called?


Solution

  • Call always using this.Method().

    If you hide the method, you'll probably want to call the new method instead of the one in the base class. On the other hand, if you make the base class' method virtual, you'll probably want to make your code to call if in a polymorphic way.

    It's hard to predict the future, but these scenarios seems more likely to happen.