Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C# method sealed or virtual by default?

Tags:

c#

inheritance

I know the definitions of virtual and sealed keywords, but if you don't use either of them with a method, can the method be overriden by default?

I am coming from vb.net background. It goes like this in vb.net (from MSDN):

If the Overridable or NotOverridable modifier is not specified, the default setting depends on whether the property or method overrides a base class property or method. If the property or method overrides a base class property or method, the default setting is Overridable; otherwise, it is NotOverridable.

I just want to know if that's also true in C#.

like image 973
Final Form Avatar asked Mar 14 '13 20:03

Final Form


1 Answers

No it cannot. You need to explicitly mark a method as virtual to allow it to be overridden in derived classes.

What you can do however, is hide a method by using the new keyword. (MSDN Documentation)

The sealed keyword is used on both class definitions and methods. It disallows inheriting from a class or overriding of a method. By default, if you don't use this keyword, others will be able to inherit from your class. (MSDN Documentation)

like image 85
Wouter de Kort Avatar answered Sep 19 '22 15:09

Wouter de Kort