> ## 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.

# C# Sealed Method

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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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`.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
