C# 15 (shipping with .NET 11) introduces native union types, closing a long-standing gap in the language. Previously developers relied on workarounds like marker interfaces, abstract base classes, nullable returns, or the OneOf library to express 'this returns one of several types'. The new syntax is concise: `public union Result<T>(T, Exception);` declares a closed set of types. Implicit conversions allow direct assignment, and pattern matching gains compiler-enforced exhaustiveness checks — if a switch expression doesn't handle all union cases, the compiler warns. This means method signatures can truthfully express all possible return shapes without exceptions for control flow. The feature is currently experimental in .NET 11 Preview 4, compiled to a struct internally, and covers type unions (not yet full discriminated unions with named cases).
Table of contents
The ProblemDeclaring a UnionCreating ValuesConsuming a UnionBack to The ProblemA Few CaveatsSummary114K Impressions5 Comments