.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Using Sealed in CSharp

December 08, 2008 By: Dave

misc_vol2_023 Another potentially useful but not very popular keyword in CSharp is the sealed keyword.  While not a keyword that you will need to use often, it is a critical keyword for framework developers who want to have tighter control over how their classes get used.

The two primary uses of the sealed keyword are as they apply to the class and as they are applied to elements of the class.

To start with the easiest first, we will apply sealed to the class.  You would apply sealed to a class when you want to create a class that no one else can inherit.  The string class is one such class.

class dmbcllc_com
{
}

sealed class blog_dmbcllc_com : dmbcllc_com
{
}

In the example above, the class dmbcllc_com can be used and inherited from anyone who can access it while blog_dmbcllc_com can only be used but cannot be inherited from since we have sealed it.

Notice that the sealed keyword comes before the class keyword.

You might also apply the sealed keyword to virtual functions that you are overriding in a child class.  This has the effect of allowing you to prevent the virtual function from being overridden in the child class past the class where it has been sealed.

class dmbcllc_com
{
    public virtual Foo() {}
}

class blog_dmbcllc_com : dmbcllc_com
{
    sealed public override Foo() {}
}

class errorClass : blog_dmbcllc_com
{
    public override Foo() {}
}

In the example above, errorClass.Foo() will cause a compile error because we are trying to override a method that has been sealed in the class above it.

You should be careful when using the sealed keyword that you do not unnecessarily seal off classes or methods simply because you can.  You may end up backing yourself into a corner.  It is possible to protect a class so much that it becomes completely unusable.  One place I’ve seen this happen before is in the Java classes that were used to render HTML.  The problem was ultimately fixed, but the initial implementation made it so that no one but the guys who wrote the classes could use them.

Seal classes and methods only when leaving them unsealed would be more dangerous than sealing them.

Other places discussing Sealed Classes:

csharp(c#) questions – what is a sealed class? a. it is a class, which cannot be subclassed. it is a good practice to mark your classes as sealed, if you do not intend them to be subclassed. how do you prevent a class from being inherited? mark it as sealed. …

C Sharp C# Interview Questions – The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class. (A sealed class cannot also be an abstract class). 11.What is Polymorphism? …

Using Sealed Class in .NET – In this article, I will discuss how to create and use sealed classes. I will also show you where programming gurus use sealed classes in their real world applications.

 

Other post in Advanced CSharp
  • Two Interfaces. Same Method. Two meanings. - September 29th, 2008
  • Making values nullable - October 9th, 2008
  • CSharp's Property Shortcuts - October 23rd, 2008
  • Readonly variables in CSharp? Really?! - October 29th, 2008
  • Dispose with Using - November 10th, 2008
  • Delegates in .NET - December 4th, 2008
  • Using Sealed in CSharp - December 8th, 2008
  • CSharp checked and unchecked - December 11th, 2008
  • Advanced CSharp - unsafe mode - December 15th, 2008
  • Volatile variables and CSharp threads - December 22nd, 2008
  • What is the global keyword in CSharp? - December 29th, 2008
  • CSharp fixed keyword - January 5th, 2009
  • using - There's more there than you are using - February 2nd, 2009
  • Stackalloc in CSharp - February 16th, 2009
  • Removing Warnings from CSharp Compile Cycle - March 10th, 2009
  • && vs & and | vs ||... What's the difference? - March 16th, 2009
  • Advanced CSharp - yield - March 25th, 2009
  • Just say “No!” to C# Regions? Really?! - April 16th, 2009
  • C# “” better than string.Empty? - April 20th, 2009
  • .Net String Pool – Not Just For The Compiler - April 22nd, 2009
  • CSharp ?? Operator - May 18th, 2009
  • Using VB.NET From CSharp - July 1st, 2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • Two Interfaces. Same Method. Two meanings.
  • Using VB.NET From CSharp
  • iTextSharp – Adding Images
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: csharp, sealed, syntax

2 Responses to “ Using Sealed in CSharp ”

  1. # 1 Dew Drop - December 9, 2008 | Alvin Ashcraft's Morning Dew Says:
    December 9th, 2008 at 10:08 am

    [...] Using sealed in C# (Dave M. Bush) [...]

  2. # 2 David Kirkland Says:
    December 18th, 2008 at 9:25 am

    Nice! I didn’t know you could seal a method!! Could be useful…

    Thanks.

← Friday Books – Beginning ASP.NET 3.5:
Native Client: Been There, Done That →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    December 2008
    S M T W T F S
    « Nov   Jan »
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.