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#.
默认情况下,C#中的方法不是虚拟的。
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 )
No. Unlike Java, in C# methods are not virtual
by default. They neither are sealed
, but you cannot override them, because they are normal methods.
No, you cannot override a non-virtual method. The new keyword only hides the base class implementation and is not a good practice.
Discussion on this with Anders Hejlsberg here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.