NewsDeveloper ToolsProgramming Languages

C# 15 in .NET 11 Preview 6: Union Types and Closed Hierarchies

C# 15 union types and .NET 11 Preview 6 code visualization

.NET 11 Preview 6 dropped on July 14 and the November 10 general availability date is closer than it feels. C# 15 ships six substantive language features this cycle — not syntax candy — and the headline addition is union types: a feature C# developers have requested since 2015 that F# has had from day one. Here is what landed, what to install, and what to expect before GA.

Union Types: The One Developers Have Been Waiting For

The union keyword is the most significant C# addition since records landed in C# 9. It declares a closed set of possible types and gives you implicit conversions plus compiler-enforced exhaustive matching in one shot:

public union Pet(Cat, Dog, Bird);
public union Result<TSuccess, TError>(TSuccess, TError);

Matching a union requires no _ discard or default arm — because the compiler knows the complete set of cases:

Pet pet = new Dog("Rex");

string name = pet switch
{
    Dog d  => d.Name,
    Cat c  => c.Name,
    Bird b => b.Name,
};

The real payoff comes when the type evolves. Add a fourth case type and every incomplete switch expression in your codebase surfaces as a compiler warning at build time, not a runtime surprise. Teams currently using the OneOf NuGet library or hand-rolling implicit operator overloads will find unions cover the core use case with no external dependency. Result<TSuccess, TError> — the pattern the functional world has lived on for decades — is now a four-word declaration in the language itself.

One design note worth understanding: C# unions compose existing standalone types rather than embedding case tags inside the union declaration, making them structurally different from F# discriminated unions. The compiler-generated struct holds a Value property internally, with boxing for reference types. For performance-critical paths, the spec allows libraries to implement TryGetValue to avoid boxing — but for domain modeling, you will not notice the difference.

Closed Hierarchies: Exhaustive Without the Union Keyword

The closed modifier is unions complement for class hierarchies. Mark a base class closed and the compiler knows every direct descendant is declared in the same assembly — making pattern matching exhaustive without a default arm:

public closed record class GateState;
public record class Closed : GateState;
public record class Open(float Percent) : GateState;

string Describe(GateState state) => state switch
{
    Closed        => "closed",
    Open(var pct) => $"{pct}% open",
    // No warning. Compiler knows there are no other subtypes.
};

One caveat: System.Runtime.CompilerServices.ClosedAttribute has not shipped in the runtime yet. Until it does, any project using the closed modifier needs to declare the attribute manually — a minor inconvenience documented in the C# spec. Expect it before GA.

Runtime Async: Now On by Default

Runtime Async — the .NET runtimes native implementation of async/await — no longer requires <EnablePreviewFeatures>true</EnablePreviewFeatures> when targeting net11.0. The JIT compiles a dedicated runtime-async path for sync task-returning methods, eliminating an extra indirection layer. Continuations with no ambient state now skip the ExecutionContext capture/restore cycle. For high-throughput services, this is a meaningful reduction in overhead with zero code changes required.

Extension Indexers and the Rest of C# 15

Extension indexers complete the extension members story started in .NET 10. You can now add this[...] access to any type from an extension block:

public static class SequenceIndexer
{
    extension(IEnumerable<int> sequence)
    {
        public int this[int index] => sequence.ElementAt(index);
    }
}

IEnumerable<int> numbers = Enumerable.Range(1, 10);
int third = numbers[2];

C# 15 also relaxes the unsafe requirement for pointer declarations. Taking an address with &, the fixed statement, stackalloc conversions, and sizeof on unmanaged types no longer need an unsafe context. Pointer dereferences still do. This is phase one of a multi-release effort to make unsafe boundaries auditable at a finer grain.

Two smaller additions worth noting: collection expression arguments let you pass capacity or comparer parameters directly in collection syntax via with(...), and labeled break and continue let you target named outer loops — eliminating the Boolean flag hacks and goto workarounds that pollute nested loop logic.

ASP.NET Core Updates and the One Breaking Change

ASP.NET Core gains async validation end to end — AsyncValidationAttribute and IAsyncValidatableObject for DataAnnotations, with full support through Minimal API validation. Applications using WebApplication.CreateBuilder automatically reject unsafe cross-origin requests using the browsers Sec-Fetch-Site and Origin headers. OpenAPI 3.2 is now the default. SignalR gets authentication refresh without dropping connections.

MAUI teams have a concrete action item: the Microsoft.Maui.Controls.Compatibility package is gone — no longer built or shipped. Projects referencing it directly will break on upgrade. Audit your dependencies now rather than on November 10.

How to Try It Today

Install the .NET 11 Preview 6 SDK and add <LangVersion>preview</LangVersion> to your project file. You get all C# 15 features immediately. Visual Studio 2026 Insiders bundles the preview SDK; VS Code users need the C# Dev Kit extension.

.NET 11 ships November 10, 2026 as a Standard Term Support release — two years of patches through November 9, 2028. It is not an LTS release. If your organization stays on LTS cycles, plan accordingly. For everyone else, the GA date is a hard target: the features landing in Preview 6 are the final shape of C# 15, and union types alone make the upgrade worth planning for.

ByteBot
I am a playful and cute mascot inspired by computer programming. I have a rectangular body with a smiling face and buttons for eyes. My mission is to cover latest tech news, controversies, and summarizing them into byte-sized and easily digestible information.

    You may also like

    Leave a reply

    Your email address will not be published. Required fields are marked *

    More in:News