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 class in C# is a class declared with the sealed modifier, which explicitly prevents other classes from inheriting from it. When a class is marked as sealed, it terminates the inheritance hierarchy for that specific type, ensuring its implementation cannot be extended or modified through derivation.

Syntax and Behavior

To create a sealed class, place the sealed keyword before the class keyword in the declaration. Any attempt to derive from a sealed class results in a compile-time error (CS0509).
public sealed class TerminalClass
{
    public int State { get; set; }
    public void Execute() { }
}

// COMPILER ERROR CS0509: 'DerivedClass': cannot derive from sealed type 'TerminalClass'
public class DerivedClass : TerminalClass 
{
}

Technical Constraints and Characteristics

  • Mutual Exclusivity with Abstract: A class cannot be both sealed and abstract. An abstract class is designed specifically to be inherited and implemented, whereas a sealed class strictly forbids inheritance. Attempting to combine them yields compiler error CS0418.
  • Value Types: In the Common Type System (CTS), all value types (defined using the struct keyword) are implicitly sealed. It is illegal to inherit from a struct, and therefore redundant (and invalid) to apply the sealed modifier to one.
  • CLR Enforcement: At the Intermediate Language (IL) level, the C# compiler translates the keyword into the sealed metadata attribute. The Common Language Runtime (CLR) enforces this restriction during the type-loading phase, making it impossible to bypass even via reflection or by writing custom IL.

Sealed Members

The sealed modifier can also be applied to methods, properties, indexers, or events. However, it is only valid on members that are overriding a virtual member from a base class. Applying sealed to an overridden member allows the class itself to be inherited, but prevents any further derived classes from overriding that specific member.
public class BaseClass
{
    public virtual void Initialize() { }
    public virtual void Process() { }
}

public class IntermediateClass : BaseClass
{
    public override void Initialize() { }

    // Overrides the base method and seals it against further overrides
    public sealed override void Process() { }
}

public class LeafClass : IntermediateClass
{
    // Valid: Initialize is not sealed in IntermediateClass
    public override void Initialize() { }

    // COMPILER ERROR CS0239: 'LeafClass.Process()': cannot override inherited member 
    // 'IntermediateClass.Process()' because it is sealed
    public override void Process() { }
}
In this context, the sealed keyword must always be paired with the override keyword. It cannot be applied to standard virtual methods at their point of initial declaration, nor can it be applied to non-virtual members, as non-virtual members are implicitly incapable of being overridden.
Master C# with Deep Grasping Methodology!Learn More