Sealed Class in C# with Example

Sealed Class in C# with Example
Sealed Class in C# with Example

If you want to prevent any class to be used as a base class in inheritance chain, you can mark that class sealed using sealed keyword.  Sealed class is normally a last class in the inheritance chain.  A sealed class can inherit from other class or interface, but other classes cannot inherit from it.  Apart from this condition, a sealed class behaves like a normal class.

The following code is just for demonstration purpose and it would not compile.

using System;

namespace Hello
{   
    //Sealed class.
     sealed class A
    {
        public void PrintName()
        {
            Console.WriteLine("This is a sealed class.");
        }

    }

    //Cannot inherit from sealed class
    class B : A
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            B _b = new B();

            //Cannot access.
            _b.PrintName();
        }
            
    }
}