C# Sealed Class


This entry is part 8 of 8 in the series C# Classes Intermediate

What is a sealed class? Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, it cannot be inherited. Sealed prevents inheritance.

When drawing a UML diagram, there can be no class under a sealed class.

Generally it is not necessary to use sealed classes at all, unless you have a very good reason for doing so.

A sealed class can be instantiated only as a stand-alone class object. It cannot be used as a base class. A sealed class is labeled with the sealed modifier. It is the opposite of an abstract class. An abstract class must be used as a base class—it cannot be instantiated as a stand-alone class object. Any attempt to use a sealed class as the base class of another class will produce a compile error.

Static classes are implicitly sealed. Also, structs are sealed. You cannot derive a class from a struct.

   public sealed class MyClass { ... }

Why Sealed Classes?

C# Corner Founder, Author, International Speaker, and Startup Adviser, Mahesh Chand says: “One of the best usage of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace. The Pens class represent the pens for standard colors. This class has only static members. For example, Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes. The Brushes.Blue represents a brush with blue color.” This quote can be found at C Sharp Corner.

You might want to use a sealed class on a class that implements security features, so that the original object cannot be “impersonated”. Also, perhaps you are writing a C# library with classes you do not want user to inherit from.

Performance may be another reason to use sealed classes. The sealed keyword tells the CLR that there is no class further down to look for methods, and that speeds things up. However, the amount that it is sped up may only be negligible.

Use Sealed More Often?

The keyword sealed is seldon used by developers, but there may be good reasons to use it more often. One such reason is that it may reveal bugs. Over at CodeProject.com, George Mamaladze in an article called C# Tweaks – Why To Use the sealed Keyword on Classes, says near the end of the article: “So using sealed keyword brings not only performance win and additional code access security but also helps to write bug free code, making better use of the .NET type safety.”

Extension Methods

You cannot inherit from the String class. It is sealed. For this reason you should know what a sealed class is because it is occasionally used in the .NET Framework. If you want to extend it (inherit from it) you need to use extension methods. Extension methods is an advanced topic that is covered in our post called C# Extension Methods.

Sealed Members

Sealed can be applied to a method. When applied, it prevents overriding of that method. The sealed modifier can only be applied to a method that is overriding a virtual method on the base class. There would be no need to apply sealed if you are not overriding a method that is declared as virtual in your base class. In the example below we show the use of a sealed method.

public class DerivedClass : BaseClass
{
   public sealed override void DoSomething()
   {
      Console.WriteLine("Doing something...");
   }
}
Series Navigation<< C# Static Class