Maarten Balliauw 6/16/2026

Discriminated unions in C# and .NET 11 (for real this time)

Read Original

This article discusses the long-awaited addition of discriminated unions to C# 15, shipping with .NET 11. It explains how developers previously relied on workarounds like ASP.NET Core's Results<> type or the OneOf NuGet package, which lacked exhaustiveness guarantees. The new union keyword allows declaring a type that is exactly one of a set of cases, with the compiler generating a value type under the hood. Implicit conversions are provided for each case type, eliminating the need for manual operator overloads. The feature supports generic unions, making it ideal for result modeling like Result<TSuccess, TError>. This is a significant update for C# developers, improving type safety and code clarity.

Discriminated unions in C# and .NET 11 (for real this time)

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week

No top articles yet