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

心不动则不痛 提交于 2019-12-05 08:10:39

That's up to you. Generally, you want to call the base class method because it might do a lot of stuff you're not privy to (especially in a class you don't control .. get it? Control.) But, if you are very confident you don't need (or want) the base class "stuff" to happen, you can remove the call.

It's simply a question of whether you want to entirely replace the behavior or add behavior.

For something like CreateChildControls, you will probably retain the call to the base class.

it depends on what you want to do. If you want the base.CreateChildControls() method to be called and then you want to perform some custom action before or after the method is called, then you can do so.

If you want to have complete control of what is happening when CreateChildControls is called, then you can simply ignore calling it altogether.

The fact that it's in there by default is just a bit of guidance for you.

It depends if you want to replace or complete the base implementation... in most cases you should call the base implementation (and you definitely should do it in the case of the CreateChildItems method...)

You might want to take a look at the template method pattern. While you can't do anything about the way the library classes are implemented, it might help you ensure the code you write is easy to use correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!