Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm surprised that there seems to be such a consensus here that non-virtual-by-default is the right way to do things. I'm going to come down on the other - I think pragmatic - side of the fence.</p> <p>Most of the justifications read to me like the old "If we give you the power you might hurt yourself" argument. From programmers?!</p> <p>It seems to me like the coder who didn't know enough (or have enough time) to design their library for inheritance and/or extensibility is the coder who's produced exactly the library I'm likely to have to fix or tweak - exactly the library where the ability to override would come in most useful.</p> <p>The number of times I've had to write ugly, desperate work-around code (or to abandon usage and roll my own alternative solution) because I can't override far, far outweighs the number of times I've ever been bitten (e.g. in Java) by overriding where the designer might not have considered I might.</p> <p>Non-virtual-by-default makes my life harder.</p> <p><strong>UPDATE:</strong> It's been pointed out [quite correctly] that I didn't actually answer the question. So - and with apologies for being rather late....</p> <p>I kinda wanted to be able to write something pithy like "C# implements methods as non-virtual by default because a bad decision was made which valued programs more highly than programmers". (I think that could be somewhat justified based on some of the other answers to this question - like performance (premature optimisation, anyone?), or guaranteeing the behaviour of classes.)</p> <p>However, I realise I'd just be stating my opinion and not that definitive answer that Stack Overflow desires. Surely, I thought, at the highest level the definitive (but unhelpful) answer is:</p> <p>They're non-virtual by default because the language-designers had a decision to make and that's what they chose.</p> <p>Now I guess the exact reason that they made that decision we'll never.... oh, wait! <a href="http://www.artima.com/intv/nonvirtual.html" rel="noreferrer">The transcript of a conversation!</a></p> <p>So it would seem that the answers and comments here about the dangers of overriding APIs and the need to explicitly design for inheritance are on the right track but are all missing an important temporal aspect: Anders' main concern was about maintaining a class's or API's implicit contract <em>across versions</em>. And I think he's actually more concerned about allowing the .Net / C# platform to change under code rather than concerned about user-code changing on top of the platform. (And his "pragmatic" viewpoint is the exact opposite of mine because he's looking from the other side.)</p> <p>(But couldn't they just have picked virtual-by-default and then peppered "final" through the codebase? Perhaps that's not quite the same.. and Anders is clearly smarter than me so I'm going to let it lie.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload