Discriminated unions are now available in .NET 11 Preview 3 as a new language feature for C#. The new 'union' keyword lets you define a type that can be exactly one of several specified types at a time, similar to F# unions or Rust enums. The feature supports named unions, ad hoc unions (e.g., int | string | bool), nullable union tracking, and a non-boxing alternative via the IUnion interface with struct-based implementations. Exhaustiveness checking in switch expressions is planned at compile time, though currently only enforced at runtime in this preview. The default implementation boxes value types, but a manual IUnion implementation can avoid this. IDE support (Rider, VS Code) is not yet available but is expected when the feature officially releases.
Sort: