.NET finally gets union types🎉
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
C# 15 / .NET 11 introduces native union types via the new `union` keyword, a long-requested feature from the functional programming world. The post covers the syntax for declaring unions, how to instantiate and deconstruct them with exhaustive `switch` expressions, and the generated implementation (a `struct` with a single `object? Value` property decorated with `[Union]`). It also explains how to write custom non-boxing union implementations using the `TryGetValue` pattern to avoid heap allocations for struct case types. Setup requirements (preview SDK, `<LangVersion>preview</LangVersion>`) and IDE support status are covered, along with upcoming related features like closed enums and closed hierarchies.