.NET (OK, C#) finally gets union types🎉 Sponsored by Dometrain Courses —Get 30% off Dometrain Pro with code ANDREW30 and access the best courses for .NET Developers Unions are one of those features that have been requested for years, and in .NET 11 (or rather, C# 15) they're finally here. In this post I describe what that support looks like, how you can use them, how they're implemented, and how you can implement your own custom types.<br>This post was written using the features available in .NET 11 preview 4. Many things may change between now and the final release of .NET 11.
What are union types?<br>Unions are one of those basic data structures which are used all the time in the functional programming world; they're available in F#, TypeScript, Rust…pretty much any functional-first language. There are many different types of union, but at their core they allow having a type that can represent two different things.<br>Some of the simplest union types are the Option and Result types. There's no "standard" version of these, but it's super common to see custom implementations. Result<> is one of the easiest to explain as it can be in one of two states:<br>Success—in this case the Result<> object contains a TSuccess value representing the "success" result for an operation that succeeded.<br>Error—in this case the Result<> object contains a TError value representing the "error" for an operation that failed.<br>You return a Result<> object from your method, and then the caller has to explicitly handle both cases instead of assuming success.<br>This pattern is often called the result pattern and it has both pros and cons in C#. I wrote a series about using this pattern, as well as considering whether it's worth it here.
Union types don't have to be the super generic form like this though. They can be used to represent any arbitrary combined set of types.<br>Union types in C# 15 with the union keyword<br>In the previous section I used the classic Result<> type as an example of a union, but unions are far more versatile than that. They're ideal whenever you want to deal with data that could be one of several potentially unrelated types.<br>For example, imagine we have three different record types, containing different properties, representing Operating Systems:<br>public record Windows(string Version);<br>public record Linux(string Distro, string Version);<br>public record MacOS(string Name, int Version);
Note that these types don't have any values in common. Prior to C# 15, the main options for handling something which could be a Windows or Linux or MaxOS object would be:<br>Try to create a base class from which all the types derive. That might work, but what if you don't control these types because they come from a library?<br>Store the type in an object instance. This works, but you lose all the safety of working with types in this case.<br>Use some "tag" value for keeping track of which type your object contains, e.g. using an enum to track this.<br>In C# 15, we get direct support for this scenario with the union keyword, as shown below:<br>// 👇 Use `union` as the type<br>public union SupportedOS(Windows, Linux, MacOS);<br>// 👆 List the types that are part of the union
You can create an instance of the SupportedOS type in a couple of ways:<br>// You can call new and pass in an instance<br>SupportedOS os = new SupportedOS(new MacOS("Tahoe", 25));
// Or you can use implict conversion (which calls new() behind the scenes)<br>SupportedOS os = new MacOS("Tahoe", 25);
The generated union type implements the IUnion interface:<br>public interface IUnion<br>object? Value { get; }
so you can always get the "inner" case value back out as an object? if you need to:<br>// You can access the stored "inner" object using `.Value`<br>Console.WriteLine(os.Value); // MacOS { Name = Tahoe, Version = 25 }
However, the canonical way to work with unions is to use a switch expression:<br>string GetDescription(SupportedOS os) => os switch<br>Windows windows => $"Windows {windows.Version}",<br>Linux linux => $"{linux.Distro} {linux.Version}",<br>MacOS macOS => $"MacOS {macOS.Name} ({macOS.Version})",<br>}; // note: no discard _ required
The switch expression automatically extracts the inner case type, and a very neat thing is that you don't need to include the _ => "discard" case either: the compiler enforces that you check for each of the allowed values, but you only need to check these values. And if you forget one, you'll get a warning:<br>warning CS8509: The switch expression does not handle all possible values of its input type<br>(it is not exhaustive). For example, the pattern 'MacOS' is not covered.
Note that if one of your case types is nullable, e.g. MacOS? then you'll need to handle null in your switch expressions too.
To come full circle, we could perhaps implement the Result<> type as the following (just an example, there's lots of different implementations we could choose!)<br>public union ResultT>(T, Exception);
or to show another classic, the Option type:<br>public record class None;<br>public union OptionT>(None,...