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

  • Two Interfaces. Same Method. Two meanings.Two Interfaces. Same Method. Two meanings. We've discussed interfaces before, but today I want to dig a little deeper.  I'm going to assume for now that you already know what an interface is and that you know how to implement one on a clas...
  • Making values nullableMaking values nullable First, a little history lesson. When .NET was first released, we had value types and object types.  The difference between the two is that you do not have to set aside memory for a value type b...
  • What is the global keyword in CSharp?What is the global keyword in CSharp? During the Christmas break, I received the following question: What does C# global:: actually do? Code example, from table adapter code: [global::System.CodeDom.Compiler.GeneratedCodeAttribute(...
  • Dispose with UsingDispose with Using I'm sure that many of you already know that many of the objects in the .NET framework need to be disposed.  The most common of these are the windows objects and the stream objects. Of course th...
  • Readonly variables in CSharp? Really?!Readonly variables in CSharp? Really?! I'm sure most of you are familiar with the keyword "const," which allows you to declare a variable and give it a value and assures that no other code will change the value. const int v = 23; p...