DEV Community

Cover image for C# 15 Unpacked: Practical Use Cases for Unions with Real Examples

C# 15 Unpacked: Practical Use Cases for Unions with Real Examples

Giorgi Kobaidze on November 23, 2026

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

The state machine example clicked for me. Enum plus nullable fields that only make sense in some states, I've seen that mess a lot.

How would you store ConnectionState with EF Core though? JSON column?

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

That's my favorite example as well.

As for your question:

EF Core can't store a union directly, so you have two choices:

JSON column (via a value converter): quick to set up, but System.Text.Json writes no type discriminator by default. Cases with the same shape can get mixed up when read back, and you can't easily query by state.

Discriminator column plus mapping, which I think is the better choice. store a StateKind column with the payload in nullable columns or JSON, and convert to and from the union in one place. You can query and index the state, and adding a new state flags your mapping code at compile time.

Keep the union in your domain code, and give the database a plain, queryable shape.

Collapse
 
umerfarooqofficial profile image
Umer Farooq •

Makes sense, the discriminator route is what I'd pick too. Being able to index the state matters more than saving a few columns.

And I'd keep StateKind as varchar with IsUnicode(false). Learned that one the hard way.

Collapse
 
lin7c profile image
xcl •

The parsing example raises an interesting tension: ParseError carries only a string Message, so a caller can display the reason but cannot branch on why parsing failed β€” distinguishing 'not a number' from 'out of range', for example, would mean parsing the message text again. Would adding a machine-readable code to ParseError, or splitting it into separate cases for each failure kind, keep the match exhaustive while letting the UI react differently to each failure? The same question applies to AccessDenied carrying a Reason string in the API example, where a 403 response might need different handling depending on the underlying reason.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Thanks for the feedback!

Collapse
 
mohammad_khalifeh profile image
Mohammad Khalifeh •

The API results section is the one I'd reach for first. I keep ending up with small hand-written result wrappers in command handlers, and a union would make the success and failure cases explicit without the extra class hierarchy. Do you return the union straight from a minimal API endpoint, or map it to HTTP responses in a thin layer first? I wrote up how I split handlers in ASP.NET Core here, in case it sits well next to this series: dev.to/mohammad_khalifeh/clean-arc...

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

You can do both - return the union or have a middleware that transforms it.

Collapse
 
mohammad_khalifeh profile image
Mohammad Khalifeh •

thank you i will consider it!

Collapse
 
kling4ai-production profile image
Kling4 AI •

The UnionsWebApi minimal API example mapping each service union case straight to an HTTP response is where unions really earn their keepβ€”much cleaner than juggling custom exceptions or nullable tuples just to separate 404 from validation errors.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Thanks for the feedback!

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

ParseError with only a string Message leaves the caller guessing which check failed. A code next to the message fixes that.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

This is just a simple example to show what's possible with unions. Of course you can add more custom types to your union, including status code.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

A status code in the union settles the error case cleanly. Add that one first.

Collapse
 
syedfahads profile image
Syed Fahad •

adding one case to a public union being a breaking change is going to catch a lot of library authors out. that's a major version bump for one line

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Good point, that’s why using unions in libraries should be done very carefully.

Collapse
 
the_daily_flare profile image
The daily flare •

I think the state machine example is the clearest use case here.

Collapse
 
georgekobaidze profile image
Giorgi Kobaidze •

Agreed, that’s also my favorite one.