user557419
user557419

Reputation:

When overriding a method

When overriding a method in c#, is it best to implement your own logic before or after the base.OverridenMethodName() ?

Upvotes: 2

Views: 89

Answers (3)

AFract
AFract

Reputation: 9723

IMHO, from a purely logical point of view, it could be "clearer" to call it before to do anything else, at the beginning of the method.

But it entirely depends of your needs and what does the base and overriden methods : according to that, you could call it at the beginning, at the middle, at the end, or not at all !

Upvotes: 0

Gaurav Deochakke
Gaurav Deochakke

Reputation: 2285

You can check these things in reflector. The framework does it's job in the InternalOnNavigatedTo method, which calls the empty OnNavigatedTo virtual method:

protected virtual void OnNavigatedTo(NavigationEventArgs e) { }

You can delete that line, it has no function, but this is not a general rule. If you don't know what are the base functions do, leave the calls there.

And hence, depending upon what the base class functionality is, you need to decide where to put you code. either before or after the call to the base method.

So its your call. Check for any trivial functionality in the base and depending upon that decide the place.

Upvotes: 3

Shlomi Borovitz
Shlomi Borovitz

Reputation: 1700

There is no "best".

Does the base.OverrideMethodName designed to be called before more specialized code?
Does it designed to be called after?
Does it even designed to be called from more specialized version?

And if you are the owner of the base class, then it's all up to your requirements.

Upvotes: 1

Related Questions