TubeSum

Union Types in C# — Step-by-Step Guide & Transcript

Union Types Are Finally Coming to C#

0h 02m video Published Jun 26, 2026 Transcribed Aug 8, 2026 M Milan Jovanović
Intermediate 2 min read For: C# developers familiar with basic language features and looking to adopt new .NET 11 capabilities.
AI Trust Score 75/100
⚠️ Average / Some Fluff

"Delivers exactly what the title promises — a clear, concise demo of union types in C# with minimal fluff."

AI 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.

[00:03]
The Problem

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.

[00:30]
Old Workarounds

Developers used tuples, the result pattern, or one-off libraries to work around this limitation.

[00:45]
Union Type Syntax

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.

[01:14]
Rewriting Methods

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.

[01:30]
Consuming Union Types

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.

[01:59]
Exhaustiveness Enforcement

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.

[02:13]
Successful Execution

When all union members are covered, both the old exception approach and the new union type approach complete successfully.

[02:26]
Further Learning

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.

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.

Mentioned in this Video

Tutorial Checklist

1 00:45 Define a union type using the syntax: 'public union UserResult { User, NotFound }' (or similar).
2 01:14 Rewrite your method to return the union type instead of throwing exceptions.
3 01:30 Consume the union type using a switch expression, handling each member type.
4 01:59 Ensure all union members are covered to avoid compiler warnings and runtime exceptions.

Study Flashcards (5)

What is the main problem union types solve in C#?

easy Click to reveal answer

They allow a method to return more than one type, making failure paths transparent to the caller.

00:03

What are the old workarounds for returning multiple types?

easy Click to reveal answer

Tuples, the result pattern, or one-off libraries.

00:30

What is the syntax for defining a union type?

medium Click to reveal answer

Use 'public union' followed by the type name, listing the member types.

00:45

How do you consume a union type?

medium Click to reveal answer

Using a switch expression that handles each member type.

01:30

What happens if you omit a union member in a switch expression?

medium Click to reveal answer

You get a compiler error, a build warning, and a runtime exception indicating non-exhaustive switch.

01:59

💡 Key Takeaways

💡

Solving a 20-year problem

Highlights the significance of union types as a long-awaited feature.

00:03
🔧

Union type syntax

Provides a concrete example of the new syntax, making it actionable for developers.

00:45
⚖️

Exhaustive switch expressions

Demonstrates a key safety benefit: compiler-enforced handling of all cases.

01:30

[00:03] 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,

[00:17] 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

[00:30] 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

[00:45] 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

[00:59] 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

[01:14] 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.

[01:30] 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

[01:45] 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

[01:59] 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

[02:13] 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

[02:26] 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#.

More from Milan Jovanović

View all

⚡ Saved you 0h 02m reading this? Transcribe any YouTube video for free — no signup needed.