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.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.
Syntax and Mechanics in Classes
When used within a class, thesealed 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 thesealed 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
sealedon a class method is strictly an extension of polymorphism. If a class method does not have theoverridemodifier, applyingsealedresults in a compiler error (CS0238). - Modifier Dependency (Interfaces): In an interface,
sealedmust be applied to a method that provides a body. Applyingsealedto an abstract interface method (one without a default implementation) results in a compiler error. - Class-Level Interaction: Because a
sealedclass cannot be inherited, its virtual and overridden methods inherently cannot be overridden further. Applying thesealedmodifier to an overridden method within asealedclass is redundant. - Properties and Indexers: The
sealedmodifier is not restricted to methods; it applies identically to overridden properties, indexers, and events.
Method Hiding (Shadowing) vs. Sealing
Whilesealed prevents a method from being overridden (maintaining polymorphic behavior), it does not prevent a derived type from hiding the method using the new keyword.
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





