Closed class hierarchies (Exploring the .NET 11 preview – Part 4)
This article explores the new "closed class hierarchies" feature in .NET 11 preview, explaining how it allows developers to define sealed base classes with a known set of derived types, enabling exhaustive pattern matching at compile time.
Background
- .NET is Microsoft's free, open-source development platform (languages like C#, runtime, libraries) used to build everything from web apps to desktop software to games. It releases yearly major versions (e.g., .NET 11 is the 2026 edition). Preview builds let developers test upcoming features.
- "Closed class hierarchies" is a C# language feature being considered for .NET 11. It lets a programmer mark a hierarchy of classes so no external code can add new subclasses — only the original author can define the allowed types. Think of it as "sealed" but for an entire family of classes.
- This matters because it gives library authors a way to make exhaustive pattern matching safe and expressive. If the compiler knows there are only N possible subtypes, it can warn you when you haven't handled all cases — similar to how Rust does enums or Kotlin does sealed classes.
- The article is part 4 of a series reviewing .NET 11 preview changes. The target audience is C# developers deciding whether to adopt preview features or looking ahead to the next major release.