DEV Community

Cover image for C# 15 Unpacked: Unions Have Arrived. Here's Why They Matter

C# 15 Unpacked: Unions Have Arrived. Here's Why They Matter

Giorgi Kobaidze on November 19, 2026

All the code in this series is available on GitHub. Clone it, run it, and try breaking it yourself. georgekobai...
Collapse
 
emilywoodsnyc profile image
Emily Woods •

The login example makes the benefit of handling every outcome concrete. I’d also give the lockout logic a clock-based test: fail three times, advance past the intended unlock time, then attempt a valid login. That checks the behaviour behind AccountLocked, separately from whether every result type has a matching branch.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Sure. But I didn’t want to complicate the example too much.

Thanks for the feedback.

Collapse
 
emilywoodsnyc profile image
Emily Woods •

That’s fair. I meant it as a follow-up exercise rather than something the introduction needed to cover. The login example already makes the union-handling point easy to follow.

Collapse
 
umerfarooqofficial profile image
Umer Farooq •

Setting CS8509 as an error is the part I’ll actually use. I tested nullable on an old project this week, and the lesson was the same: a warning nobody is forced to fix usually doesn’t get fixed.

The default struct hole is a good catch too. The compiler narrows the gaps, it doesn’t close all of them.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

That’s a useful setting. It’ll make sure you don’t accidentally skip any of the possible options.

Collapse
 
csm18 profile image
csm •

Union is like a T-shirt that is one fit for all specified sizes.Like brothers share a T-shirt only with each other not any outside person. And only one can wear it at a time! πŸ˜„

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

That is a brilliant analogyπŸ˜„

Collapse
 
paullovelace profile image
Paul Lovelace •

the bit about var and _ matching the union itself caught me off guard β€” I'd have assumed a switch arm always unwraps, so that's exactly the thing I'd get wrong on first use. the login example sells it though, a new case turning into a warning instead of a runtime crash is the whole point

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Thanks!

Collapse
 
hasnan_khan_dev_0 profile image
Hasnan Khan •

This is exciting! One thing I find particularly interesting is how unpacked unions could make state handling more predictable by moving certain mistakes from runtime to compile time.
It reminds me of Flutter's sealed classes, where explicitly modeling loading, success, and error states makes applications easier to maintain. The real win isn't just writing less code; it's making invalid states harder to represent in the first place.
Now I'm curious: could this change how C# developers approach API responses and complex state management?

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Thanks!

I think not immediately, not to break existing code, but after some time we might see some changes in how all that is handled.

Collapse
 
eye_java_420f1faa10ae8b86 profile image
Nan •

One gap I'd watch when adopting this: the exhaustiveness guarantee lives on switch expressions. Rewrite the same dispatch as a switch statement or an if/else chain and the compiler stays quiet when a fourth case type appears later, the code just falls through. I reproduced it on the .NET 11 preview SDK after reading your repo, and two other writeups on C# 15 unions note the same limitation.

Is an analyzer for the statement form on the roadmap, or is the expectation that style rules push everyone to expressions? In a codebase I maintain we lint for expression-only dispatch over closed hierarchies, and it has caught silent fall-throughs twice. Your login example ported cleanly, by the way.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Hm... this is interesting, to be honest, I haven't thought about that yet. Thank you!

Collapse
 
kling4ai-production profile image
Kling4 AI •

Having compiler-checked exhaustive matching on a closed list of types instead of maintaining a throwaway default arm for 'impossible' subclasses is huge. Comparing the before-and-after login flow makes the boilerplate drop obvious right away.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Agreed. The new approach is so much better.

Collapse
 
nullandvoid_ profile image
Jyanthi •

A most commendable advancement for the C# fraternity! The true merit of union types, methinks, lies not merely in tidier code, but in sparing developers from the peril of overlooked possibilities. Having the compiler itself call attention to unhandled cases is a considerable boon, particularly in projects that grow and evolve over time. A fine exposition, George; I should be most interested to witness how this feature fares in larger, more intricate applications.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Thank you!

I guess we're gonna find out when we start using it for real! And I wonder how .NET 12 will take this further.

Collapse
 
argumentmoney8117 profile image
ArgumentMoney8117 •

The login example makes the case well: adding TwoFactorRequired and getting a compiler warning at every switch is the real payoff, not just shorter code. One decision rule I would like to hear more about is when you would still choose a closed hierarchy over a union. Unions cannot add instance fields, so shared behavior has to live elsewhere, and a sealed abstract record can carry it. Is that the main boundary for you, or is there another case where the older pattern still wins?

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

The new pattern mostly wins.

Collapse
 
alexshev profile image
Alex Shev •

Union types pay off when the exhaustiveness story reaches the boundary where errors and domain alternatives are handled. The value is not just fewer nulls; it is making an unhandled case difficult to compile and easy to locate during review.\n\nExamples that contrast a nullable or exception-heavy API with a closed result union would make the practical benefit especially clear for teams deciding where to introduce them first.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

You're correct, but that "\n\n" looks kinda suspicious 😁