When overriding virtual methods, is it best to use the base method?

I noticed that when overriding virtual methods in C # using Visual Studio, the IDE automatically adds a call to base.Method (). On the other hand, when overriding abstract methods, the IDE automatically adds NotImplementedException ().

Why does VS automatically add a call to base.Method () when overriding virtual methods? Is it recommended to use the base method?

+4
source share
8 answers

It depends if you still need the base behavior to happen. This decision will be made on a case-by-case basis. There is no hard and fast rule, although some patterns would expect a call to the base method (the correct implementation of the IDisposable pattern works this way)

+7
source

Visual Studio automatically generates a base class call. It is up to you to choose whether it is advisable to call the base, it depends on your reason for overriding.

+4
source

If you completely redefine the method in question, it can be argued that you have a higher risk of not following the Liskov signature principle. But there is no general rule.

0
source

An abstract method is basically an empty method. The IDE adds an unrealized exception to notify that this method has not yet been implemented, and it cannot delegate to the base class, since it will not have an implementation even there. | In the case of a virtual method, the IDE will by default invoke the base class implementation of this method, since it will have an implementation at least there (which you will probably override)

0
source

It depends entirely on what you need to do. Often you need to include the behavior of the β€œbase” method in the β€œchild” method, so VS helps add syntax for this. However, this is not necessary in all situations.

On the other hand, the implementation of an abstract method means the absence of a β€œbase” method that you can name (after all, it is abstract!), So the best VS can simply β€œmark” a method as not implemented.

0
source

Consider the case when you use the te VS code generator, but later forget (or decide not to) fill out the body of the method.

In case you redefine the virtual method, if you do not want to change the behavior in any way, it makes sense to keep the old one - what the base call does.

On the other hand, although if you are override a abstract , you do not have a base for reference - abstract methods do not have a body by definition. Therefore, if you were unable to provide a new implementation when overriding, it is always a mistake. The exception NotImplemented attracts your attention.

There are several templates in which you must call a method (for example, IDisposable or some cases of a template method). In other cases, it is always advisable to provide a new implementation, so it depends on the scenario.

0
source

the calling method depends on what you want to do:

for example, if you override the OnInit method of a control, the Init event will not raise by default unless you invoke the base method. if you want the behavior of the change method to be executed, you should not call the base method. for example, the overridde method ToString () of the StringBuilder class has its own implementation and never calls the ToString of the base class (object)

I think Visual studio offers a basic call, because without other modifications it still works (the basic bheaviuor does not change) the abstract method MUST be implemented, and throwing an exception as a default instruction should help you restore the correct implementation

0
source

By adding such a call, it does not change the behavior of the method.

-3
source

Source: https://habr.com/ru/post/1382277/


All Articles