~/blog/dotnet-11-csharp-15-union-types
Back to Blog

article

.NET 11: Why I Want to Try C# 15 Union Types

With .NET 11 released, C# 15 union types are on my list to explore: clearer application outcomes, compiler checks, and the questions I want to test in APIs.

Laptop and open notebook on a wooden desk beside a window in soft daylight, with an out-of-focus screen

.NET 11 arrived on November 10. The feature I am particularly curious about is union types in C# 15.

After years of working with .NET, I still enjoy finding a language feature that makes me want to build a small example and see how it feels. This is one of those features. I have not tested it in my own applications yet; these are the questions I want to explore.

Making API Outcomes Explicit

Consider a lookup in an application. It might find the requested item, find nothing, or receive an invalid identifier. Those are different outcomes, and I want the code calling that operation to be able to see them clearly.

C# 15 union types let a value represent one of a declared set of types. Switch expressions can use compiler exhaustiveness checks to help identify missing cases. That interests me because the result type can tell a caller which outcomes it needs to consider.

For a lookup, I would start with three deliberately small cases: an item found, an item not found, and invalid input. I want to see whether handling those cases remains easy to follow when the operation and its callers change.

The application result and the HTTP response still need separate thought. I would make the mapping to status codes and response bodies explicit, then check what the client actually receives. A clearer C# type is useful, but I also want a contract that makes sense to someone consuming the API.

What I Want to Try

My first experiment would be a small endpoint with a caller and tests for each outcome. I would then add another outcome and inspect what the compiler reports at the call sites. That should give me a better feel for the feedback than reading a feature list.

I also want to inspect the generated OpenAPI document and try a client against it. Microsoft’s release notes describe JSON support for unions in ASP.NET Core and schemas that describe the alternatives. I would check the actual payloads, particularly where two cases have similar shapes, before choosing a public contract.

Finally, I would compare it with the result types I would normally write. Would someone reviewing the code understand the operation more quickly? How much work would a change create for callers? Would introducing it in an existing codebase be worth that change?

A Small Experiment First

That is where I want to start: one operation, a few outcomes, and enough tests to understand the behaviour.

I enjoy seeing .NET continue to develop, and I like having something concrete to investigate after a release. Union types have made my list this time. Once I have tried them, I will have a better basis for deciding where I would use them.

Which .NET 11 feature do you want to try first?

Back to all posts