Type Union is coming to C#

published on 2024/07/29

It would be better if C# gave you a way to declare a type that allows you to store one of a limited number of other types in the same place, and let it do all the hard work guarding the variables for you.

Many other languages already do this. They typically call these special types discriminated unions, tagged unions, sum types or type unions. All of them solve the problem of allowing a single variable to hold values of one or more limited forms.

It is time C# had a feature that did this too.

C# Design Team

It will arrive in 4 forms

  • Standard Union
  • Specialized (Zero Allocation) Union
  • Ad Hoc Union
  • Custom Union

There is no commitment yet from the design team on when we will see the implementation of these new features.