sealed Keyword in C#
The sealed keyword is one of those C# features that developers see often but rarely think deeply about. It quietly influences inheritance, performance, and API design — especially in large systems and frameworks.
This guide explains what sealed does, why it exists, and when you should (and shouldn’t) use it, with practical examples and real-world scenarios.
What Does sealed Mean?
The sealed keyword prevents a class from being inherited or prevents a virtual method from being overridden further.
You can apply sealed to:
- A class
- A method (when overriding a virtual method)
1. Sealed Classes
A sealed class cannot be inherited.
Example
public sealed class Logger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
Trying to inherit from it:
public class CustomLogger : Logger {}
// ❌ Error: cannot derive from sealed type
Why seal a class?
- To prevent misuse through inheritance
- To lock down behavior
- To improve performance (JIT optimizes sealed classes)
- To protect API design in libraries
2. Sealed Methods
You can also seal a method, but only when overriding a virtual method.
Example
public class Base
{
public virtual void Process() {}
}
public class Child : Base
{
public sealed override void Process()
{
// Implementation
}
}
public class GrandChild : Child
{
public override void Process() {}
// ❌ Error: cannot override sealed method
}
Why seal a method?
- To stop further overrides
- To preserve behavior
- To avoid breaking logic in subclasses
Why Does sealed Improve Performance?
When a class is sealed:
- The JIT compiler knows no subclass will override methods
- It can devirtualize calls
- It can inline methods
- It can optimize dispatch
This makes sealed classes slightly faster, especially in tight loops or high-frequency calls.
This is why many .NET framework classes (e.g., String) are sealed.
Real‑World Scenarios
Scenario 1: Utility classes
Utility classes like Math, String, or Path are sealed because:
- They are not meant to be extended
- Inheritance would cause confusion
- Behavior must remain consistent
Scenario 2: Preventing fragile inheritance
If you expose a class in a public API, consumers might inherit from it in unexpected ways.
Sealing the class protects your design.
Scenario 3: Performance‑critical components
High-performance libraries often seal classes to allow JIT optimizations.
Scenario 4: Sealing methods in a hierarchy
You may want to allow inheritance but restrict certain behaviors.
public class Animal
{
public virtual void Move() {}
}
public class Bird : Animal
{
public sealed override void Move()
{
// Birds fly
}
}
Now no subclass of Bird can change how Move() works.
When NOT to Use sealed
- When you expect developers to extend your class
- When you’re building a framework that encourages inheritance
- When sealing would limit testability (mocking frameworks often rely on inheritance)
Interview‑Ready Summary
-
sealedprevents inheritance - Sealed classes = no subclassing
- Sealed methods = no further overrides
- Sealing improves performance via JIT optimizations
- Use it to protect API design, prevent misuse, and optimize performance
Top comments (0)