Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

non virtual by default

So if “non virtual by default” is the right thing for c#... Then why isn’t “sealed by default” the right thing too?

It seems to me that if we are supposed to be testing extensibility points and purposefully allowing derivation and overriding for specific scenarios, then it seems to me that classes should be sealed by default, until we know that we acknowledge those scenarios.

Now, I’m not taking a stance here on the virt/nonvirt issue here. I really don’t care. But it just seems to me that “sealed by default” is the logical extension to the c# team’s points for their design, and I’m genuinely curious as to why they chose a different model at the class derivation level.

Comments

William Bartholomew said:

I would assume this is so that outside programmers can easily extend classes written by other people unless they have explicitly taken steps to stop them.
# May 19, 2004 4:42 AM

Ramon Smits said:

The sealed keyword has no performance penalty for not using it. The reason that virtual isn't the default is really only a performance consideration.

Using sealed as default would be option in the security design of .net

Using sealed normally has only value for classes used in communication between components or in scenarios where you want to be flexible to change the implementation of your class so that inherited class cannot rely on specific flow or state of a class. That is the reason why a lot of framework classes are sealed.
# May 19, 2004 7:11 AM

TrackBack said:

# May 19, 2004 10:00 AM

JD said:

My $.02 is that it's it's a granularity issue. In terms of member methods, not making them virtual ensures that a subclass type exhibits the same behavior as their parent -- If B is a subclass of A, you know that the inherited members don't break the contract set out by A.

At the class level, however, you don't break a contract if you subclass B from A and add NEW members to the programmatic interface.

So from the client code perspective -- if I've got a B reference, I can use the additional, more specialized members but still know that "B is an A" for the inherited functionality. From the over-used analogy department... If I create an Auto type and give it a non-virtual Start() method for the engine, you can create a subclass Truck. Client code knows that Start() will always start the engine and can't have been overridden to turn on the CB Radio.

Or maybe I'm wrong. :)
# May 19, 2004 12:13 PM

Lance said:

Andy,

I have been toying with the same question while working on my C# Coding Standards document.

Its obvious that only methods/properties that have been tested for extensibility should be designated Virtual.

However, the correllary for a non-Sealed Class has different reprocussions. The only harm in leaving a class non-Sealed is if there is critical initialization in the constructor, since any derived class has the option to omit calling the base constructor. Otherwise, I see no harm in leaving the class unSealed.

However, when that class is a subclass, the Sealed designation becomes very important. There are numerous times when you want to break the inheritance chain for security or maintenance purposes.

So, as a result, I think the Sealed keyword is very important, but there is no useful way to apply a blanket rule to say "Always apply Sealed...".

# May 19, 2004 1:05 PM

TrackBack said:

# May 19, 2004 6:35 PM

Johnny Whackoff said:

non-virtual by default was a way to beat java in benchmarks.

Virtual methods are slower to invoke that NonVirtual ones.

It's just a example of Microsoft holding developers back while showing up competition with false performance

[/conspiracy]
# May 20, 2004 12:48 AM

Thomas Eyde said:

I still wonder how I test for extensibilty. This argument is often raised when virtual by default is debated.

So do you:
1. Add virtual to the method.
2. Add override to the other method.
3. Run the app, see what happens or..?

I can't figure this out, for to test on this you must have an extension scenario. It's impossible to know and test on every scenario.

Why is it so bad to let me test on my scenario and decide it it works or not? I know my scenario better than anybody else, and I can see if it works or not.

But I really like to know how I should test for extension.
# May 21, 2004 10:42 AM

GregS57 said:

I like it the way it is.

# February 24, 2008 8:46 PM

Lexapro safety. said:

Lexapro weight gain. Lexapro. Lexapro withdrawal.

# July 18, 2008 3:43 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)