David Lanouette <[EMAIL PROTECTED]> wrote:

> Anybody know why methods aren't virtual by default in .NET?  It seems like a
> really bad default to have all methods non-virtual.

If you consider the design and maintenance implications, it's actually
an incredibly bad idea to have methods default to virtual. It's a great
feature-point for C# that it didn't follow Java's example in this area,
IMO.

Virtual methods are quite difficult to design properly. They require
two, independent, sets of semantics: one for callers and another for
implementers. Designing a class to be robust and secure in the face of
malicious or naive overriding - something which essentially introduces
external code in the middle of the class's implementation - is not
trivial.

Inheritance is probably the closest coupling that can be created between
two classes, and we all know why coupling is bad when designing robust,
modular systems. Virtual methods make coupling even closer, because one
class replaces methods in the other.

It's harder to change the implementation of a class which has lots of
virtual methods, because descendants are more closely coupled to the
ancestor. That's where the maintenance and versioning problems come in.

This is quite independent of the performance implications, of course,
which are themselves not irrelevant: the performance of managed code has
historically been one of its most significant weaknesses.

I believe virtual methods should be kept to a minimum and only
introduced when necessary.

-- Barry

-- 
http://barrkel.blogspot.com/

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to