Should a C# team seal classes by default or leave them open for inheritance, and how would you enforce the choice?
answer
- methods are closed by default, classes are not
- the second API subclasses see
- which change is breaking later
- CA1852 and an .editorconfig severity
basics
~20 sSeal application and internal classes by default, because unsealing later is not a breaking change while sealing a published class later breaks every subclass. For public library types decide per type, and enforce internal sealing with CA1852.
solid answer
~40 sC# members are non-virtual unless declared `virtual`, but classes are open unless declared `sealed`. An open class carries a second contract, its `protected` and `virtual` surface, that subclasses depend on. The decision is asymmetric: removing `sealed` later is safe, while sealing a published class breaks derived code with CS0509 on recompile and `TypeLoadException` for already-built subclasses. So I would seal internal and application classes by default and enforce it with CA1852 in `.editorconfig`. For public library types I would decide per type, sealing whatever was not designed for inheritance and using `sealed override` to lock individual invariants. The Framework Design Guidelines warn against sealing public classes without a reason, so that decision is written down, not automatic.
go deeper
Recall what sealed does to a class and that C# methods are non-virtual unless declared virtual, while classes are open unless sealed.
Explain the inheritance contract an open class carries through protected and virtual members, and what CS0509 and sealed override each prevent.
Know that sealing a released public class breaks existing subclasses at compile time and at load time, and use CA1852 to find internal types that can safely be sealed.
Own the default: sealed for code the team controls, a per-type decision for public APIs, enforced by analyzer severity and review, with a written path for opening a type later.
## What the decision is C# already makes **members** opt-in for overriding: a method is non-virtual unless it says `virtual`. **Classes** are the opposite: a class that is neither `static` nor `sealed` is open to derivation by anyone who can reach one of its constructors. A team standard has to decide whether new classes start `sealed` and are opened deliberately, or start open and are sealed when someone notices a reason. There is no single right answer; the right default differs between application code and a published library. ## What sealed buys - **No inheritance contract to maintain.** An open class with `protected` members and `virtual` methods has a second API, the one subclasses see. Once someone derives from it, changing the order in which your methods call each other can break them. A sealed class has only its public surface to keep stable. - **Clearer intent and stricter compiler checks.** In a sealed class a new `virtual` member is error **CS0549** and a new `protected` member is warning **CS0628**, so accidental extension points cannot appear. - **Tool support.** Code analysis rule **CA1852**, "Seal internal types", introduced with .NET 7, reports a type that is not visible outside its assembly and has no subtypes in it. It is not enabled by default in .NET 10, and it skips assemblies that grant `InternalsVisibleTo` unless configured otherwise. Its documentation's stated reason is performance: sealing types can improve it. - **Room to change your mind.** Removing `sealed` in a later version is not a breaking change: everything that compiled against the sealed class still compiles and runs. ## What sealed costs - **Consumers lose an extension point.** A caller who wants a convenience subclass, or a test double built by subclassing, cannot have one. They must wrap the class instead, or you must add an interface. - **Sealing later is a breaking change.** If a public class ships unsealed and a later version seals it, every subclass stops compiling with **CS0509**, "cannot derive from sealed type". A subclass that was already compiled is worse off: when it loads against the new library, the runtime throws **`TypeLoadException`** ("the parent type is sealed"). - **Framework guidance leans the other way for public frameworks.** The Framework Design Guidelines, as reprinted in the .NET documentation from their 2008 edition, say not to seal classes without a good reason, because users inherit for reasons the designer did not foresee. That advice targets reusable libraries with unknown consumers, not application internals. ## A rule that holds up | Code | Suggested default | Why | |---|---|---| | Internal and application classes | `sealed` | Every subclass would live in the same codebase, so opening a class later is a local edit | | Public types in a shared or NuGet library | Decide per type, write it down | Sealing later is breaking; opening later is not, so start sealed only when extension is not part of the design | | Classes designed as bases | `abstract` or open, with chosen `virtual` members | Make the extension points explicit and keep the rest non-virtual | | Overrides in a published hierarchy | `sealed override` where subclasses must not change behaviour | Locks an invariant without closing the class | The trade is asymmetric, which is what makes "sealed by default" defensible for code you own: **unsealing is cheap and safe, sealing is expensive and breaking.** For a public API the same asymmetry argues for sealing anything not explicitly designed for inheritance, while accepting the guideline's warning that you will sometimes refuse a legitimate use. ## Enforcing it 1. Turn on **CA1852** at warning or error severity in the repository's `.editorconfig` (`dotnet_diagnostic.CA1852.severity`) so internal types that nobody derives from are flagged. 2. For public types, make "sealed or designed for inheritance?" a review checklist item, since CA1852 deliberately covers only types that are not visible outside the assembly. 3. When a team needs to subclass a sealed type for tests, prefer extracting an interface or a seam over unsealing the production class. 4. Record the decision in the coding standard, with the exception path: who may unseal, and the note that a published type may never be sealed again without a major version. ## What not to claim - Sealing is not a security boundary on its own; it controls derivation, not access. - Performance is a secondary argument. The JIT can avoid some virtual-call overhead when it knows a type is sealed, but that is a runtime detail to measure in a benchmark, not the reason a team standard should rest on. - `sealed` does not affect `virtual` members inherited from base classes: a sealed class can override `ToString` or `Equals` like any other.
- Your team wants to subclass a sealed production class to fake it in tests. What do you do?Do not unseal the class just for tests. Extract an interface the consumers depend on, or move the hard-to-test behaviour behind a seam such as an injected collaborator, and fake that. Subclass-based fakes also couple tests to the class's internal call order, which is exactly the inheritance contract sealing avoids.
- Does sealing a class make calls to its methods faster?It can: when the JIT knows the exact type it may turn some virtual calls into direct ones, and CA1852's documentation cites performance as its reason. The effect depends on the call site and is a runtime detail, so measure it with a benchmark. Base the team rule on the API contract, with performance as a bonus.
- Says sealing a published class in a later version is a non-breaking change.
- Thinks C# methods are virtual by default, so sealing classes is the only protection.
- Claims a sealed class cannot override inherited members such as ToString.
- Treats performance as the main reason to seal, without measuring it.
- Applies framework-design advice for public libraries unchanged to internal application code.