Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I mark all methods virtual?

In Java you can mark method as final to make it impossible to override.

In C# you have to mark method as virtual to make it possible to override.

Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to be overridden), since most likely you don't know in what way your class can be inherited?

like image 275
Georgii Oleinikov Avatar asked Jan 22 '13 03:01

Georgii Oleinikov


People also ask

Is it necessary to override a virtual method?

Virtual Methods: Few Points To Remember If a method has the same definition in both the base and derived class then it's not required to override the method. Override is only required if both have a different definition.

Are all methods in C# virtual?

By default, methods are non-virtual. We can't override a non-virtual method. We can't use the virtual modifier with the static, abstract, private or override modifiers.

What does making a method virtual do?

A method is declared as virtual by specifying the keyword "virtual" in the method signature. A virtual method may or may not have a return type. Virtual methods allow subclasses of the type to override the method. They are used to implement run time polymorphism or late binding.

Is it mandatory to override virtual methods of base class?

Yes, you need to use the override keyword, otherwise the method will be hidden by the definition in the derived class.


1 Answers

In C# you have to mark method as virtual to make it possible to override. Does it mean that in C# you should mark all methods virtual (except a few ones that you don't want to be overridden), since most likely you don't know in what way your class can be inherited?

No. If the language designers thought that virtual should have been the default then it would have been the default.

Overridablility is a feature, and like all features it has costs. The costs of an overrideable method are considerable: there are big design, implementation and testing costs, particularly if there is any "sensitivity" to the class; virtual methods are ways of introducing untested third-party code into a system and that has a security impact.

If you don't know how you intend your class to be inherited then don't publish your class because you haven't finished designing it yet. Your extensibility model is definitely something you should know ahead of time; it should deeply influence your design and testing strategy.

I advocate that all classes be sealed and all methods be non-virtual until you have a real-world customer-focussed reason to unseal or to make a method virtual.

Basically your question is "I am ignorant of how my customers intend to consume my class; should I therefore make it arbitrarily extensible?" No; you should become knowledgable! You wouldn't ask "I don't know how my customers are going to use my class, so should I make all my properties read-write? And should I make all my methods read-write properties of delegate type so that my users can replace any method with their own implementation?" No, don't do any of those things until you have evidence that a user actually needs that capability! Spend your valuable time designing, testing and implementing features that users actually want and need, and do so from a position of knowledge.

like image 66
Eric Lippert Avatar answered Sep 18 '22 14:09

Eric Lippert