LobstersSunday · May 24, 2026FREE

.NET (OK, C#) finally gets union types

csharpdotnetunion-typeslanguage-design

In .NET 11 preview 2, released on May 23, 2026, C# finally gains union types, a feature that lets developers define a type that can hold one of several distinct cases. This is a significant addition to the language, addressing a long-standing gap compared to languages like F#, Rust, or TypeScript. Union types in C# are implemented as a new kind of type declaration using the `union` keyword, with each case defined as a member. The compiler ensures exhaustive handling via pattern matching, reducing runtime errors. For example, a `Result<T, E>` union can represent success or failure without exceptions or null checks. This feature is part of the broader .NET 11 preview, which also includes improvements to Native AOT and ASP.NET Core. Developers can try it by installing the preview SDK from dotnet.microsoft.com. The move is expected to simplify error handling, state machines, and domain modeling in C# codebases.

// why it matters

Union types reduce boilerplate and bugs by making impossible states unrepresentable.

Sources

Primary · Lobsters
▸ Read original at andrewlock.net

Like this? Get the next digest.