---
title: 'Union Types Are Finally Coming to C#'
source: 'https://youtube.com/watch?v=El9M8suevAU'
video_id: 'El9M8suevAU'
date: 2026-08-08
duration_sec: 159
---

# Union Types Are Finally Coming to C#

> Source: [Union Types Are Finally Coming to C#](https://youtube.com/watch?v=El9M8suevAU)

## Summary

This video introduces union types, a new feature in .NET 11 and C# that allows methods to return multiple distinct types, solving a long-standing problem in expressing failure paths. The presenter demonstrates how union types replace older workarounds like tuples and the result pattern, and shows how to use them with switch expressions for exhaustive handling.

### Key Points

- **The Problem** [00:03] — For about 20 years, C# lacked a good way to express that a method could return more than one thing. Methods like 'GetUserOrThrow' could return a user or throw an exception, but this wasn't transparent to the caller.
- **Old Workarounds** [00:30] — Developers used tuples, the result pattern, or one-off libraries to work around this limitation.
- **Union Type Syntax** [00:45] — Union types are introduced in .NET 11. The syntax uses 'public union' followed by the type name, similar to a primary constructor, but defines a closed set of types that are part of the union.
- **Rewriting Methods** [01:14] — The original method can be rewritten to return a union type, e.g., 'UserResult', which can be either a 'User' instance on the happy path or a 'NotFound' instance on the failure path.
- **Consuming Union Types** [01:30] — Union types are consumed using a switch expression. You define behavior for each member type (e.g., 'User' or 'NotFound'). The switch is exhaustive; omitting a member results in a compiler error.
- **Exhaustiveness Enforcement** [01:59] — If a union member is omitted, the compiler produces a build warning and a runtime exception indicating the switch expression is non-exhaustive. This is current as of .NET 11 preview 5.
- **Successful Execution** [02:13] — When all union members are covered, both the old exception approach and the new union type approach complete successfully.
- **Further Learning** [02:26] — The presenter recommends a Build session by Mads and Dustin for more details on union types in C#, with a link in the video description.

### Conclusion

Union types in .NET 11 provide a cleaner, more expressive way to handle multiple return types, improving code clarity and safety through exhaustive switch expressions.

## Transcript

this fixes something that has annoyed us for like 20 years. Let me show you with an example. Up until now, we didn't have a good way of expressing that a method could return more than one thing. If we look at this get user or throw method,
it can return a user in the happy path or throw an exception in the failure path. Now, this isn't transparent to the caller. They don't know that this throws an exception until they actually run into an exception and we used to go
around this with things like tuples, implementing the result pattern, or maybe using the one-off library. However, this all changes with .NET 11 and the introduction of union types. A union type lets us express a type that
consists of a closed set of types and to give you an example of that, you say give you an example of that, you say public union, this is the syntax, and we're going to create a user result. The syntax is similar to using a primary
constructor, except here you just define the types that are part of the union. And now that we have our union type, let's see how this improves our code. We can rewrite our original get user or throw method to the more expressive
version returning a user result, so we can return union types from methods and this can either be a user instance on the happy path or a not found instance in the failure path. And to consume a union type, you use a switch expression.
So, switch over the union instance and you consume the individual types. So, you define what happens if the instance is a user or what happens if the user is not found. The great thing is that this is exhaustive and if you omit a union
member, you're going to get a compiler error. So, let's say I leave out not found on purpose and I try to run our demo, you'll see that we get a build warning. This is current as of .NET 11 preview five and we also get a runtime
exception that our switch expression is non-exhaustive, and that we didn't cover the not found path. If I return this back in our code, and I rerun the example, you will see that both the old exception approach and the new union
type approach complete successfully. If you want to learn more about working with union types in C#, I highly recommend taking a look at the build session from Mads and Dustin that goes over this in much more detail. I'm going
to leave the link to this in the description of this video as well as the know in the comments what you think about the upcoming union types that we about the upcoming union types that we are getting with the next version of C#.
