When calling methods on a base class from a derived class, should the 'base' keyword be used? It seems using the base keyword would increase code readability but for me so far, when I exclude it, there is no affect on code compilation and execution.
The base keyword is important when overriding methods:
override void Foo()
{
base.Foo();
// other stuff
}
I never use it for anything else.
You should not use base
unless you specifically mean "Even if there is a method in this class that overrides the base implementation, I want to call the base implementation and ignore the one on this class".
Using base
bypasses the virtual dispatch mechanism that is so important in polymorphism by causing a call
instruction to be emitted rather than callvirt
.
So saying base.Foo()
is very, very different in semantics to saying this.Foo()
. And you almost always want the latter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With