.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.

•13m read time•From andrewlock.net
Post cover image
Table of contents
What are union types?Union types in C# 15 with the union keywordUsing union types in .NET 11How are union types implementedAvoiding boxing with custom Union implementationsWhat other features are yet to come?Summary
1 Impression