C# 15 introduces native union types (discriminated unions / sum types) via the `union` keyword, shipping as a preview in .NET 11 Preview 2 (April 2026) with GA expected November 2026. A union composes existing standalone types into a closed set, enabling compiler-enforced exhaustive pattern matching without a `default` arm. Under the hood, the generated type is a `record struct` with a single `object? Value` property, which means value types get boxed — but developers can hand-write a custom `[Union]` `record struct` with `HasValue`/`TryGetValue` members to avoid boxing. Unions support generics and body declarations with helper methods. Compared to the `OneOf` NuGet package, the native `union` keyword moves exhaustiveness checking from runtime to compile time. Features still pending include union member providers (accessing shared properties directly), uninitialized-field analysis, and closed hierarchies/enums.

11m read timeFrom blog.ndepend.com
Post cover image
Table of contents
C# Union Syntax: The Plan That ShippedHow C# Unions Work Behind the ScenesThe Default Value and the Null ArmThe UnionAttribute and IUnion ContractAdding a Body to a UnionCould C# Unions be Smart for Value Type Only Unions?What Is Not Yet in C# 15 UnionsC# Unions vs OneOf, Class Hierarchies and EnumsC# Unions FAQConclusion
26 Impressions