Skip to main content
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.

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.

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.
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.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More