Sealed Classes in C# Explained

What is a Sealed Class?

Sealed Class in C# is a feature by which you can restrict your class being inherited by other classes. So if you don’t want your class to be used as a base class to be derived from, then use the Sealed feature.

Sealed classes are locked and cannot be inherited. You can just use them as they are. A class can be sealed in C# using the keyword sealed.  So you cannot use a Sealed class as a Base Class and for the same reason it cannot be an abstract class. Because Sealed classes cannot be used as a base class it can be slightly faster during runtime to call a Sealed class.

In Visual Basic .NET, NotInheritable keyword serves the purpose of sealed.

If you have ever noticed, structs are sealed. You cannot derive a class from a struct. 

Even though you cannot inherit from a sealed class, you can sure instantiate a sealed class and create an object of it and use their methods. Here is an example of that

    sealed class MySealedClass

    {

        public int x;

        public int y;

    }

 

    class MySealedTest2

    {

        static void Main()

        {

            MySealedClass sc = new MySealedClass();

            sc.x = 110;

            sc.y = 150;

            Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);

        }

    }

    // Output: x = 110, y = 150

 

 

How to Define a Sealed Class in C#

A class can be declared as sealed by using the keyword sealed before the Class name in C# as shown below.

public sealed class MyClass

{

    // Class members here.

}

 

 

Example of a Sealed Class in C#

    sealed class MySealedClass

    {

        public int x;

        public int y;

    }

 

    class MySealedTest2

    {

        static void Main()

        {

            MySealedClass sc = new MySealedClass();

            sc.x = 110;

            sc.y = 150;

            Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);

        }

    }

    // Output: x = 110, y = 150

 

The above example of a sealed class works fine, but if you attempt to Inherit from a sealed class by using a statement like this

    class MyDerivedC : MySealedClass

    {

        //This will not work

    }

It will not work and will throw the following error message

'MyDerivedC' cannot inherit from sealed class 'MySealedClass'

 

When to use Sealed Class?

Why use a Sealed Class?
Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, this class cannot be inherited.
In C#, the sealed modifier is used to define a class as sealed. If a class is derived from a sealed class, compiler throws an error.

Sealed classes can be used in instances where you would need a Singleton Class. I can think of a Sealed class to do things like accessing private database or any service which should not be tampered and changed by the classes using it, so I can just have a sealed class which does everything to access the service or database and the user of the class can just use it and not attempt to change it. So it helps in encapsulating your logic.

Sealed classes seem to have a better performance during runtime, so that could be another reason to use a sealed class.

So what are the benefits of a Sealed Class?

1.       It encapsulates your logic and makes it available only for usage and not for alteration so your class and logic remains intact across.

2.       It has a better performance during .Net Runtime.

Sealed Method

You cannot declare a Method as Sealed, but if you can derive from a base class and override a method in the derived class, you can declare that method as sealed.

So even though you cannot create a sealed method directly, you can do if you inherit a class and then override a method and declare it as sealed as in the following example.

Example 1:
In the following example, MyDerived2  inherits from MyDerived but MyDerived2 cannot override the virtual function F that is declared in MyBase and sealed in MyDerived.

class MyBase

    {

        protected virtual void F() { Console.WriteLine("MyBase.F"); }

        protected virtual void F2() { Console.WriteLine("MyBase.F2"); }

    }

    class MyDerived : MyBase

    {

        sealed protected

Tags: