When classes are not being inherited from. Is there an advantage to be
gained by making them "sealed", and, if so, what is that advantage?
Thank you,
Adrian.
Jeroen - 17 Nov 2006 11:00 GMT
> Adrian < wrote:
> When classes are not being inherited from. Is there an advantage to be
> gained by making them "sealed", and, if so, what is that advantage?
Maybe there's an advantage to think of, but as I learned it you seal
classes to make sure nothing inherits from it, not that you seal
classes that are not inherited from.
-Jeroen
Ciaran O''Donnell - 17 Nov 2006 11:08 GMT
The could be a performance advantage as if the CLR KNOWs for definate that
nothing is going to inherit and override methods it can make performance
optimizations like methods inlining etc without any risk. It depends on you
code and its structure and what optimizations your framework version has in
it but it something shouldn't be inherited from then mark it as sealed.
Is a class can be a base, make sure to mark only methods/fields that need to
be protected and virtual as such.
Ciaran O'Donnell
> When classes are not being inherited from. Is there an advantage to be
> gained by making them "sealed", and, if so, what is that advantage?
>
> Thank you,
> Adrian.
Bryan - 17 Nov 2006 14:47 GMT
It simplifies the virtual method table. At compile time, it is not possible
to determine the actual method that will be called on a virtual method.
Because of this, the CLR has to do a virtual table lookup. There is some
overhead in doing this. If a class is sealed, then it may be possible to
avoid this. Something like this.
ParentOfSealedClass p = new ParentOfSealedClass();
p.SomeVirtualMethod();
SealedClass s = new ParentOfSealedClass();
s.SomeVirtualMethod();
In this case, when p.SomeVirtualMethod(), it is possible that p could be a
subclass of ParentOfSealedClass and the method call cannot be optimized. But
s.SomeVirtualMethod() can be optimized. SealedClass cannot have children.
The exact methods that are called on an instance of SealedClass are known at
compile time. Because of this, s.SomeVirtualMethod() will be a little faster.
Also keep in mind that you can seal just a method or property in a class.
This will do exactly the same kind of thing, just on a method or property
basis.
> When classes are not being inherited from. Is there an advantage to be
> gained by making them "sealed", and, if so, what is that advantage?
>
> Thank you,
> Adrian.
ssamuel - 17 Nov 2006 15:56 GMT
Adrian,
Aside from the technical and performance benefits, it prevents
inheritance. If you're distributing a binary package to customers, you
can use sealed to make sure that they can't tamper with things that
should be untouched.
Stephan
> Adrian < wrote:
> When classes are not being inherited from. Is there an advantage to be
> gained by making them "sealed", and, if so, what is that advantage?
>
> Thank you,
> Adrian.
> Adrian < - 17 Nov 2006 18:33 GMT
> <snipped > you seal classes to make sure nothing inherits from it, not
that you seal
> classes that are not inherited from.
>
> -Jeroen
"Ciaran O''Donnell" <CiaranODonnell@discussions.microsoft.com> wrote in
message news:C5D36332-058A-412D-BF42-400C92193F09@microsoft.com...
> There could be a performance advantage <snipped>
> Ciaran O'Donnell
> It simplifies the virtual method table <snipped>
> Adrian,
>
> <snipped> you can use sealed to make sure that they can't tamper with
things that
> should be untouched.
>
> Stephan
***************************************
M a n y t h a n k s,
Adrian