Should I call the base class implementation when overriding a method in C # for ASP.NET?

I understand that overriding a method / function redefines its implementation in a derived class from its implementation in the base class.

Now, what bothers me is if I override the class in ASP.NET, for example CreateChildControls() (I randomly selected it for no particular reason), VS2008 automatically generates:

 protected override void CreateChildControls() { base.CreateChildControls(); } 

Good enough, the default implementation simply calls the base class CreateChildControls() .

So, if I want to run some code, since I do not know how base.CreateChildControls() , I have to do this:

 protected override void CreateChildControls() { /*My Code Here*/ base.CreateChildControls(); } 

or ignore what base.CreateChildControls() , and just do

 protected override void CreateChildControls() { /*My Code Here*/ } 
+6
source share
5 answers

This is for you. Typically, you want to call a base class method because it can do a lot of things that you are not attached to (especially in a class that you don’t control .. get it? Control.) But if you are very confident in yourself, you don’t need to (or not want) the base class "stuff" so you can remove it.

+11
source

It is simply a question of whether you want to completely replace behavior or add behavior.

For something like CreateChildControls, you'll probably save the base class call.

+3
source

it depends on what you want to do. If you want to call the base.CreateChildControls () method, and then you want to perform some kind of custom action before or after the method call, you can do this.

If you want full control over what happens when CreateChildControls is called, you can simply ignore it altogether.

The fact that it is there by default is just a small guide for you.

+1
source

It depends on whether you want to replace or complete the base implementation ... in most cases you should invoke the base implementation (and you should definitely do this with the CreateChildItems method ...)

0
source

You can take a look at the template template . Although you can’t do anything about how library classes are implemented, it can help you ensure that the code you write is used correctly.

0
source

All Articles