When using some framework/api, sometimes it's pretty unclear if you must call base.Method if you override it, for example you can be pretty sure that you should call base.Maethod() when you are overriding event invocater, to propagate the event, in other situations it can be not so clear especially when there is no source code available, and no comments.
I wounder how other programmers decide should they call base method or not in this situation, and if you are about to write some framework how to inform other programmers that you expect base method to be called or not in virtual members.
getMethod("myMethod"). getDeclaringClass(); If the class that's returned is your own, then it's not overridden; if it's something else, that subclass has overridden it.
The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class.
The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method.
Nowadays I don't think that consumers of a class that override a method should ever need to call base.Method(). The code should be written in such way that it cannot be broken.
public class MyBase { private void FooInternal() { DoRequiredStuff(); Foo(); } public virtual void Foo() {} }
If you are requiring that consumers of your class MUST implement functionality of a particular method, that method should be marked abstract.
If consumers of your class should optionally provide functionality of a particular method, that method should be virtual.
There is really no way to require that a consumer of a class call a base.Method() on a virtual method. It really depends on context. If the base.Method() does some work that you'd otherwise have to do, it'd behoove you to call base.Method() if that would save you some development time/it makes sense in that context.
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