C# Reference ยท Classes and Object-Oriented Concepts
sealed and inheritance design
Design guide to sealed in C#: sealed classes, sealed override members, sealed records, when to seal, performance effects and designing for inheritance.
The sealed modifier stops inheritance. On a class it means nobody can derive from it. On an overriding method, property or event it means derived classes can no longer override that member, while the class itself can still be inherited. Structs are implicitly sealed, and a static class is sealed and abstract at once.
The design question behind sealed is whether you intended the class to be a base class. Inheritance is a promise: subclasses depend on your internal behavior, call order and protected members, so you cannot change them freely later. Unless you design and document for extension, it is safer to seal the class and offer composition or interfaces instead. See the keyword page sealed for the syntax alone.
public sealed class Invoice { } public override sealed string ToString() => "...";
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[access] sealed class Name { } [access] sealed record Name(...); class Derived : Base { public sealed override void Method() { } public sealed override int Property { get; } }
| Use | Effect |
|---|---|
sealed class |
No class can inherit from it. Cannot be combined with abstract or static. |
sealed override |
Valid only on a member that overrides a virtual member. Stops further overriding. |
sealed record |
A record that cannot be derived from. Also valid on a record's ToString (C# 10) to stop derived records regenerating it. |
| Struct | Implicitly sealed; the keyword is not allowed. |
Errors: deriving from a sealed type gives CS0509 ('TurboEngine': cannot derive from sealed type 'Engine'), and overriding a sealed member gives CS0239 ('Leaf.Run()': cannot override inherited member 'Middle.Run()' because it is sealed).
Usage notes
- Seal by default for classes you did not design for extension, especially public types in libraries. Unsealing later is a compatible change; sealing later is a breaking one.
- Performance: the JIT can call methods on a sealed type directly (devirtualize them) and make
is/cast checks against it a simple type-handle comparison. The gain is real but small; do not seal only for speed. sealed overrideis for the middle of a hierarchy. It closes one member while leaving the rest open. It rarely matters outside framework-like code.- Testing and mocking. Mocking frameworks usually subclass the type. Put an interface in front of a sealed class instead of removing
sealed. - Prefer composition over inheritance. Hold a collaborator in a field and forward calls; you control exactly what is exposed.
- Design for inheritance when you unseal. Document which virtual methods are called when, keep constructors free of virtual calls, and make protected members a small, stable surface.
- Abstract classes cannot be sealed, since they exist to be derived. A sealed class that implements an interface is fine and common.
- Closed hierarchies: C# has no
sealed+permitsas in some languages. A private nested class hierarchy or aprivate protectedconstructor limits who can derive; exhaustive switches need a_arm. - Reflection:
Type.IsSealedistruefor sealed classes and all value types, as the first example shows.