C# 15 Adds Closed Hierarchies and Collection Expression Arguments as .NET 11 Preview 5 Ships
The June 9 .NET 11 Preview 5 brings a closed class modifier for exhaustive switch checking and with(...) collection expression arguments, alongside the first runtime scaffolding for C# union types.
Overview
Microsoft shipped .NET 11 Preview 5 on June 9, 2026, adding two new C# 15 language features to the in-development compiler: a closed class modifier that lets the compiler verify switch exhaustiveness, and collection expression arguments that pass constructor parameters through a with(...) element. The release, Microsoft’s .NET Blog says, “includes improvements across the .NET Runtime, SDK, libraries, ASP.NET Core, .NET MAUI, C#, Entity Framework Core, and more.”
The C# 15 work continues a theme The Machine Herald covered when union types made their first in-SDK appearance in Preview 3. Preview 5 layers on the surrounding pattern-matching machinery and, per Microsoft’s documentation, brings the first runtime support for the union feature.
What We Know
According to Microsoft Learn, C# 15 now lists three headline features: collection expression arguments, union types, and closed hierarchies. The language ships with .NET 11, and preview SDK versions support it.
The closed-hierarchy feature is the most structurally significant of the additions. “Starting in C# 15, you can apply the closed modifier to a class to declare a closed hierarchy,” the documentation states, and “a closed class can only be derived from within its declaring assembly, which fixes the set of direct descendants at compile time.” Because the compiler then knows every direct descendant, Microsoft Learn notes that “a switch expression that handles each one is exhaustive and doesn’t need a default arm.” The modifier carries strict rules: it is “a contextual keyword,” a closed class “is implicitly abstract and can’t be combined with sealed, static, or an explicit abstract modifier,” and derivation “isn’t transitive” — a non-closed descendant can still be subclassed in other assemblies unless it is also marked closed. The same rules appear in the dotnet/core release notes, which independently state that “a closed class is implicitly abstract” and that “direct subtypes must be declared in the same assembly as the closed base class.”
The second new feature, collection expression arguments, lets developers configure a collection’s underlying constructor inline. “You can pass arguments to the underlying collection’s constructor or factory method by using a with(...) element as the first element in a collection expression,” Microsoft Learn explains, enabling a programmer to “specify capacity, comparers, or other constructor parameters directly within the collection expression syntax.” The documentation shows passing a capacity to a List<T> and an OrdinalIgnoreCase comparer to a HashSet<T> without a separate constructor call.
Union types, introduced earlier in the preview cycle, gained runtime backing in this build. “The runtime includes the UnionAttribute and IUnion types beginning with .NET 11 Preview 5,” according to Microsoft Learn. The broader .NET 11 overview similarly lists “discriminated-union scaffolding (UnionAttribute and IUnion) in System.Runtime.CompilerServices” among the library additions.
Beyond the language, the same overview records that runtime-native async “no longer requires <EnablePreviewFeatures>true</EnablePreviewFeatures> for projects that target net11.0,” a step toward making the feature a default.
What We Don’t Know
Several C# 15 capabilities remain incomplete. Microsoft says “some features from the proposal specification aren’t yet implemented” for unions and “are coming in future previews.” The closed-hierarchy feature also ships ahead of its runtime support: “In C# 15 preview 5, the runtime doesn’t yet ship System.Runtime.CompilerServices.ClosedAttribute,” so projects using the modifier must declare the attribute themselves for now, per Microsoft Learn.
The features are previews and subject to change. According to the .NET 11 overview, ”.NET 11 is currently in preview” and “the final release is expected in November 2026.”