How to Use Sealed Classes in C# to Prevent Inheritance

In C#, object-oriented programming relies heavily on inheritance, but there are scenarios where restricting a class from being extended is critical for design integrity, security, or performance. This article explains how to use the sealed keyword in C# to prevent class inheritance, demonstrates how it behaves with code examples, covers sealing specific overridden methods, and outlines the primary architectural benefits of this approach.

Declaring a Sealed Class

To prevent other classes from inheriting from a class, apply the sealed modifier before the class keyword during the class declaration. Once sealed, no other class can use it as a base class.

public sealed class SecurityTokenManager
{
    public string GenerateToken(string userId)
    {
        return $"Token_{userId}_{Guid.NewGuid()}";
    }
}

If another class attempts to inherit from SecurityTokenManager, the C# compiler produces a compile-time error:

// This will cause compiler error CS0509: 
// 'DerivedManager': cannot derive from sealed type 'SecurityTokenManager'
public class DerivedManager : SecurityTokenManager
{
}

Because the error occurs at compile time rather than runtime, you catch unintended architectural deviations immediately during development.

Sealing Overridden Methods and Properties

The sealed modifier is not limited to entire classes. You can also apply it to specific methods or properties that override a base class member. This allows derived classes to inherit from the class while preventing them from further overriding that specific member.

To seal a method, use sealed in conjunction with override:

public class BaseService
{
    public virtual void Execute()
    {
        Console.WriteLine("Standard execution.");
    }
}

public class SpecializedService : BaseService
{
    // Sealed override prevents further modification down the inheritance tree
    public sealed override void Execute()
    {
        Console.WriteLine("Specialized execution locked from further overrides.");
    }
}

public class FinalService : SpecializedService
{
    // This will cause compiler error CS0239: 
    // 'FinalService.Execute()': cannot override inherited member 'SpecializedService.Execute()' because it is sealed
    public override void Execute() 
    {
        Console.WriteLine("Attempting to override.");
    }
}

When to Use Sealed Classes

  1. Security and Immutability: When building libraries or security-sensitive utilities (such as cryptography handlers, authentication tokens, or mathematical algorithms), sealing a class ensures that downstream consumers cannot alter or subvert core behaviors by overriding methods.
  2. Preventing Broken Invariants: If a class is not specifically engineered or tested to serve as a base class—often adhering to the principle that classes should be "designed and documented for inheritance or else prohibited"—sealing it prevents improper extension.
  3. Performance Improvements: The .NET Just-In-Time (JIT) compiler can optimize calls to members of sealed classes. Because the runtime knows that no derived class can override a member, it can bypass virtual method table lookups (devirtualization) and directly inline the method calls, resulting in minor performance gains.