Closed class hierarchies in .NET 11
.NET 11 Preview 4 introduces closed class hierarchies, a new feature that allows developers to restrict inheritance by marking classes with the `closed` modifier, preventing any further subclassing from that point in the hierarchy. This provides more explicit control over type design, improving API clarity and runtime performance through guarded dispatch.
Background
- .NET is Microsoft's free, cross-platform development framework (replacing the older .NET Framework); new major versions ship annually, and "Preview 4" means this feature is still experimental.
- C# is the main programming language used with .NET; a "closed class hierarchy" is a proposed language/compiler feature that lets a developer mark a base class so the compiler knows every possible subclass — enabling "exhaustive" pattern matching (the compiler can check that all cases are handled, like a switch statement that must cover every option).
- Prior context: C# already has "sealed" classes (no subclasses allowed) and "discriminated unions" (a way to define a type as exactly one of a fixed set of cases). Closed hierarchies sit in between: you can subclass, but only within the same assembly/project, and the compiler knows all subtypes.
- Why it matters: This closes a long-standing gap for C# developers who want the safety of exhaustive matching (common in F#, Rust, Swift) without the restrictions of sealed classes or the complexity of full discriminated unions. The feature is not final; this post analyzes the preview design.