Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to finalize a virtual method in C#?

By that I mean, it is possible to mark a virtual method in C# as final so no other types deriving from this type, can override it ever again?

like image 754
Joan Venge Avatar asked Jan 12 '11 20:01

Joan Venge


People also ask

Can virtual method be empty?

The solution with empty virtual methods gives no information on what is supported by a specific document instance. Furthermore, it forces you to add all possible methods in the base class. This forces all clients to be updated as whel, when you decide that the base class needs additional methods.

Can a virtual method be called?

The derived classes inject their own implementation and definition of the virtual method. This means that in your application, you are able to call a method on a base class and cause its derived class's method to be executed.

Are methods virtual by default?

By default, methods are non-virtual. You cannot override a non-virtual method. Virtual properties behave like virtual methods, except for the differences in declaration and invocation syntax. It is an error to use the virtual modifier on a static property.


2 Answers

Yes: when overriding a method you can also declare it sealed.

like image 157
Jeff Sternal Avatar answered Oct 14 '22 08:10

Jeff Sternal


You can use sealed.

like image 43
Aaron McIver Avatar answered Oct 14 '22 08:10

Aaron McIver