. Topicala Page Index Token

A Journal about the experiences I have developing little applications in C#, Perl, Html and Javascript and talking about things new things that I use. Always Geeky; Always Nerdy; Always poor Grammer!

I am a Software Analyst Developer working in Southport, England but living in Liverpool. I develop mainly in C# and ASP.Net. I have been developing comercial software for several years now. I maintain this site (hosted at SwitchMedia UK) as a way of exploring new technologies (such as AJAX) and just generally talking about techie geek issues. This site is developed through a host of Perl scripts and a liberal use of Javascript. I enjoy experimenting with new technologies and anything that I make I host here.

Quick Search

Web www.kinlan.co.uk

Tuesday, February 14, 2006

A Quick C# Question: Sealed classes

If you have a sealed class are all the methods inside the sealed class also typed as sealed.

I am asking this because I have some virtual functions inside an abstract class that I would like to seal, I don't want the class to be extended so sealing the class makes sense. Therefore if I just seal the class can all the methods be considered sealed by the JIT'er or do I have to seal the methods too.

Technorati Tags
[feed], [feed], [feed], [feed], [feed]

Related Wikipedia Documents
, , , , , , , ,

My Related Documents
,

Related Amazon Books
Professional ASP.NET 2.0: / Programming C#: / Pro C# 2005 & the .NET 2.0 Platform: / Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries: / Windows Forms Programming in C#: /

Related Images From Flickr

Comments: [Add New]

Hi,

From what I understand, if you seal a class, it can not be derived at all - if you try to inherit from it, you will get a compile-time error. Also, when sealing on a method by method basis, you can only seal methods which are an override of an inherited method.

Abstract classes, on the other hand, can not be instantiated - you must create a sub-class.

So, logically, you can not have an abstract class that is sealed - abstract requires that you sub-class, sealed prevents you from doing so.

Sealing a method also prevents name hiding. In other words, even if you declare a public new void Foo() in a derived class, if the parent Foo() is sealed, your new method will be ignored.

So in answer to your question: yes, sealing a class seals all of its methods. But, you may want to rethink your desire to seal the methods of an abstract class in this case because unless your abstract class is in fact a derivation of some other class, you will not be able to seal its methods individually.

By Anonymous Guy Sherman, at Wednesday, December 17, 2008 10:46:00 PM