Using Sealed in CSharp

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.

Related Post

2 Responses to “Using Sealed in CSharp”

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail