Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A sealed method in C# is a method that prevents derived types from overriding its implementation. In the context of classes, it is applied to an overridden virtual method to terminate the virtual dispatch chain for all subsequent subclasses. In the context of interfaces (C# 8.0 and later), it is applied to a method with a default implementation to make it non-virtual, preventing derived interfaces or implementing types from providing their own overrides.

Syntax and Mechanics in Classes

When used within a class, the sealed keyword must be paired with the override keyword. It cannot be applied to a base virtual method, an abstract method, or a standard non-virtual method.
public class BaseClass
{
    // 1. The base method must be virtual (or abstract)
    public virtual void Execute()
    {
        Console.WriteLine("Base implementation");
    }
}

public class DerivedClass : BaseClass
{
    // 2. The derived method overrides and seals the implementation
    public sealed override void Execute()
    {
        Console.WriteLine("Sealed implementation");
    }
}

public class LeafClass : DerivedClass
{
    // 3. COMPILER ERROR CS0239: Cannot override inherited member because it is sealed
    // public override void Execute() 
    // { 
    // }
}

Syntax and Mechanics in Interfaces

In C# 8.0 and later, interface members are virtual by default. Applying the sealed modifier to an interface method with a default implementation removes its virtual nature. Unlike in classes, the sealed keyword here is used directly without the override modifier.
public interface IBaseInterface
{
    // 1. Sealed default interface method
    public sealed void Execute()
    {
        Console.WriteLine("Sealed default implementation");
    }
}

public interface IDerivedInterface : IBaseInterface
{
    // 2. COMPILER ERROR CS8506: Cannot implement a sealed interface member
    // void IBaseInterface.Execute() { }
}

public class ImplementingClass : IBaseInterface
{
    // 3. COMPILER ERROR CS8506: Cannot implement a sealed interface member
    // Explicit interface implementation is required to attempt an override, which fails.
    // void IBaseInterface.Execute() { }

    // Legal: Because the interface method is sealed, it does not participate in interface mapping.
    // This public method simply exists as an independent method that hides the interface method.
    public void Execute() { }
}

Technical Constraints

  • Modifier Dependency (Classes): The compiler enforces that sealed on a class method is strictly an extension of polymorphism. If a class method does not have the override modifier, applying sealed results in a compiler error (CS0238).
  • Modifier Dependency (Interfaces): In an interface, sealed must be applied to a method that provides a body. Applying sealed to an abstract interface method (one without a default implementation) results in a compiler error.
  • Class-Level Interaction: Because a sealed class cannot be inherited, its virtual and overridden methods inherently cannot be overridden further. Applying the sealed modifier to an overridden method within a sealed class is redundant.
  • Properties and Indexers: The sealed modifier is not restricted to methods; it applies identically to overridden properties, indexers, and events.

Method Hiding (Shadowing) vs. Sealing

While sealed prevents a method from being overridden (maintaining polymorphic behavior), it does not prevent a derived type from hiding the method using the new keyword.
public class ShadowingClass : DerivedClass
{
    // Legal: This hides the inherited method rather than overriding it.
    public new void Execute()
    {
        Console.WriteLine("Shadowing implementation");
    }
}
In the shadowing scenario, calling Execute() on a reference of type ShadowingClass invokes the new method. However, calling it on a reference of type BaseClass or DerivedClass will invoke the implementation in DerivedClass. This behavior occurs because the new keyword creates a completely new, unrelated method that does not participate in the original virtual dispatch chain. When invoked via a base reference, the runtime resolves the call to the most derived override in the original hierarchy, ignoring the shadowed method. This dispatch behavior is entirely dictated by the mechanics of the new keyword; the exact same routing would occur even if the method in DerivedClass were a standard override and not marked sealed.
Master C# with Deep Grasping Methodology!Learn More