This question really is kinda pointless, but I’m just curious:
This:
public sealed class MyClass
{
protected void MyMethod(){}
}
compiles, but gives a warning
while This:
public sealed class MyClass
{
public virtual void MyMethod(){}
}
doesn’t compile. Just out of sheer curiosity, is there a reason for this?
The only reason I can think of is that sometimes you would need to write protected methods to override other protected methods. The language could have been designed to allow this:
but not this
but that might have been seen to be a little hard to follow – it’s the absence of
overridewhich makes it useless, whereas in the case ofit’s the presence of
virtualthat is useless. The presence of something “wrong” is probably easier to understand than the absence of something useful.In this case, being virtual may also have performance implications, whereas making something protected instead of private probably doesn’t – so it’s a bit more severe.
These are just guesses though really – if we’re really lucky, Eric Lippert will give a more definitive answer. He’s the one you want, not me 🙂
Best answer: treat warnings as errors and they’re equivalent anyway 😉