When should you call base.Method() in overridden method, and how to mark this when you write code in team?

Alex Burtsev picture Alex Burtsev · Jan 18, 2011 · Viewed 9k times · Source

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.

Answer

Alex Burtsev picture Alex Burtsev · May 5, 2012

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() {}
}