Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Seald classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
> Adrian < - 17 Nov 2006 09:52 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.