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.