Seald classes

  • Thread starter Thread starter > Adrian
  • Start date Start date
A

> Adrian

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 said:
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
 
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
 
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.
 
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
 
Jeroen said:
<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 said:
There could be a performance advantage <snipped>
Ciaran O'Donnell

Bryan said:
It simplifies the virtual method table <snipped>

ssamuel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top