C# Feature - Union types
Union types
Section titled βUnion typesβUnion types represent a value that can be one of several case types, with compiler support for conversions and exhaustive switching.
Introduced In
Section titled βIntroduced InβThis feature is documented as part of C# 15 preview. The official Microsoft Learn page is dated March 20, 2026 and says C# 15 preview is available with the .NET 11 preview SDK and Visual Studio 2026 Insiders. The same page notes that union types first appeared in .NET 11 Preview 2.
Why This Feature Exists
Section titled βWhy This Feature ExistsβC# has long supported inheritance hierarchies, records, and pattern matching, but it did not have a first-class language construct for βexactly one of these cases.β Union types make that intent explicit.
Example
Section titled βExampleβpublic record class Cat(string Name);public record class Dog(string Name);public record class Bird(string Name);
public union Pet(Cat, Dog, Bird);Usage:
Pet pet = new Dog("Rex");
string name = pet switch{ Dog d => d.Name, Cat c => c.Name, Bird b => b.Name,};When To Use It
Section titled βWhen To Use Itβ- Use it when a model should be exactly one of a small number of known alternatives.
- Use it when exhaustive switching is part of correctness.
Common Pitfalls
Section titled βCommon Pitfallsβ- This feature is still preview-only as of March 20, 2026.
- The Microsoft docs note that some runtime support and proposal details were still incomplete in early .NET 11 previews.