C# 15 is here with .NET 11, and one of the features that caught my attention immediately is union types.

If you have worked with F#, TypeScript, Rust, Kotlin, or other languages with sum types or discriminated unions, the idea will feel familiar.

But the interesting question is not:

“What is the syntax?”

The more useful question is:

When does this actually improve a real C# codebase?

Let’s look at it without the bla bla.

The Problem Before C# 15

Imagine a service that looks up a product.

The operation can return one of two meaningful outcomes:

  • the product exists
  • the product does not exist

Before C# 15, you had several options.

You could return null.

public Product? FindProduct(int id) { // ... } 

Simple, but now null carries business meaning.

You could return object.

public object FindProduct(int id) { // Product or ProductNotFound } 

That is worse because the compiler cannot tell callers which types are valid.

You could introduce a base class:

public abstract record ProductLookupResult; public sealed record Product(int Id, string Name) : ProductLookupResult; public sealed record ProductNotFound(int Id) : ProductLookupResult; 

This is much better.

But sometimes the participating types do not naturally belong in the same inheritance hierarchy.

And sometimes that hierarchy exists only because you need a method to return multiple possible results.

That is exactly where union types become interesting.

Enter C# 15 Union Types

In C# 15, you can declare a union explicitly:

public sealed record Product(int Id, string Name); public sealed record ProductNotFound(int Id); public union ProductLookup( Product, ProductNotFound ); 

Now the type system says:

ProductLookup can contain a Product or a ProductNotFound.

Nothing else.

You can then write:

ProductLookup result = new Product(42, "Mechanical Keyboard"); 

The conversion from one of the declared case types happens naturally.

Then pattern matching becomes the really interesting part:

string message = result switch { Product product => $"Found: {product.Name}", ProductNotFound missing => $"Product {missing.Id} was not found" }; 

Because the compiler knows the complete set of cases, it can check whether the switch is exhaustive.

That is the part I care about most.

Why Exhaustiveness Matters

Imagine you later extend the union:

public sealed record ProductUnavailable(int Id); public union ProductLookup( Product, ProductNotFound, ProductUnavailable ); 

Now an older switch such as:

string message = result switch { Product product => $"Found: {product.Name}", ProductNotFound missing => $"Product {missing.Id} was not found" }; 

no longer handles every possible case.

The compiler can tell you.

That turns a category of runtime problems into compile-time feedback.

For me, that is the real value of union types.

Not shorter syntax.

Not novelty.

Better modeling of valid states.

Where I Would Actually Use Them

I would not replace every class hierarchy in an application with unions.

But there are several places where I think they fit naturally.

1. Service Results

A service method often has multiple legitimate outcomes.

For example:

public sealed record User(int Id, string Email); public sealed record UserNotFound(int Id); public sealed record UserSuspended(int Id); public union UserLookupResult( User, UserNotFound, UserSuspended ); 

Then:

public UserLookupResult GetUser(int id) { // domain logic } 

This is much more expressive than:

public User? GetUser(int id) 

because null cannot tell you whether the user:

  • does not exist
  • is suspended
  • is hidden
  • is unavailable

A union makes those outcomes part of the contract.

2. Minimal API Results

This is probably one of the most interesting use cases for backend developers.

Imagine a Minimal API endpoint:

app.MapGet("/products/{id:int}", (int id) => { ProductLookup result = GetProduct(id); return result switch { Product product => Results.Ok(product), ProductNotFound missing => Results.NotFound(new { missing.Id }) }; }); 

Now the domain result and the HTTP response are separate concerns.

The service says:

Product or ProductNotFound 

The API layer decides:

200 or 404 

That separation is healthy.

3. Commands and Operations

Consider an operation such as creating a user.

Possible results might be:

public sealed record UserCreated(Guid Id); public sealed record EmailAlreadyExists(string Email); public sealed record InvalidUser(string Reason); public union CreateUserResult( UserCreated, EmailAlreadyExists, InvalidUser ); 

Now the method signature tells the caller what can happen:

public CreateUserResult CreateUser(CreateUserCommand command) { // ... } 

That is valuable documentation built directly into the type system.

4. State Machines

Union types can also work well for application states.

For example:

public sealed record Loading; public sealed record Loaded<T>(T Data); public sealed record LoadFailed(string Error); public union LoadState<T>( Loading, Loaded<T>, LoadFailed ); 

Then:

string Render(LoadState<Product[]> state) => state switch { Loading => "Loading...", Loaded<Product[]> loaded => $"Loaded {loaded.Data.Length} products", LoadFailed failed => $"Error: {failed.Error}" }; 

This kind of model makes invalid states harder to represent.

And that is usually a good thing.

Union Types vs Closed Hierarchies

C# 15 also introduces closed hierarchies, and the two features are related but not identical.

A union is useful when the case types do not necessarily share inheritance.

public union SearchResult( Customer, Order, Product ); 

Those types can remain completely independent.

A closed hierarchy is better when the types naturally belong to the same inheritance tree.

public closed record class PaymentState; public record class Pending : PaymentState; public record class Completed : PaymentState; public record class Failed(string Reason) : PaymentState; 

So my mental model is:

Union Types → unrelated types → one value can be one of them Closed Hierarchies → related types → controlled inheritance tree 

Why Not Just Use OneOf or a Result Library?

This is a fair question.

Libraries such as OneOf and various Result<T> implementations have solved similar problems for years.

And they are still useful.

The difference is that union types are now part of the language itself.

That means:

  • compiler understanding
  • language-level pattern matching
  • exhaustive checking
  • cleaner syntax
  • no external abstraction required

For teams already using a mature Result library, I would not rewrite an entire codebase just because C# 15 exists.

But for new APIs and new domain models, native union types are definitely worth evaluating.

What I Would NOT Use Union Types For

There are a few situations where I would avoid them.

When one result is simply optional

If a lookup legitimately means:

value or nothing 

then T? may still be perfectly fine.

Do not create:

public union MaybeProduct( Product, NoProduct ); 

just because you can.

When you already have a meaningful domain hierarchy

If the types naturally share behavior and inheritance, a normal or closed hierarchy may communicate the model better.

When exceptions represent exceptional failures

I would not automatically convert every exception into a union case.

A database connection failure and a ProductNotFound result are not necessarily the same category of problem.

Business outcomes and infrastructure failures should remain conceptually distinct.

A More Realistic Example

Here is a simple API-oriented example:

public sealed record Order( Guid Id, decimal Total ); public sealed record OrderNotFound(Guid Id); public sealed record OrderAccessDenied(Guid Id); public union GetOrderResult( Order, OrderNotFound, OrderAccessDenied ); 

Service:

public GetOrderResult GetOrder( Guid orderId, Guid userId) { var order = FindOrder(orderId); if (order is null) { return new OrderNotFound(orderId); } if (order.UserId != userId) { return new OrderAccessDenied(orderId); } return new Order( order.Id, order.Total ); } 

API:

app.MapGet( "/orders/{orderId:guid}", ( Guid orderId, Guid userId) => { GetOrderResult result = GetOrder(orderId, userId); return result switch { Order order => Results.Ok(order), OrderNotFound notFound => Results.NotFound( new { notFound.Id }), OrderAccessDenied => Results.Forbid() }; }); 

What I like here is that the service signature communicates the business outcomes clearly.

A caller cannot accidentally assume:

this always returns an Order 

because the type says otherwise.

The Bigger Picture

For several C# releases, the language has been moving toward more expressive modeling.

Records.

Pattern matching.

Primary constructors.

Extension members.

And now union types and closed hierarchies.

The common direction is clear:

make more information visible to the compiler.

Instead of writing comments saying:

This method can return A, B, or C. 

we can increasingly express that directly in the type system.

That makes APIs easier to understand and makes refactoring safer.

My Take

Union types are one of the C# 15 features I expect to actually use.

But selectively.

I would reach for them when:

  • a method has several legitimate business outcomes
  • those outcomes should be visible in the signature
  • the types do not naturally share inheritance
  • exhaustive pattern matching adds safety

I would not use them simply to avoid if statements or because they are new.

The best language features are usually the ones that make the code say more clearly what the domain already means.

And union types can do exactly that.

What’s Next?

I recently covered the wider .NET 11 and C# 15 release on Tech No Bla Bla, including:

  • union types
  • closed hierarchies
  • ASP.NET Core 11
  • performance improvements
  • MCP
  • AI agents

🎥 Watch the video:
https://youtu.be/RjAdbFz9e8A

I also published a deeper written overview on Medium:

“.NET 11 Is Here — What’s New in C# 15, ASP.NET Core, Performance, and AI Agents.”

Medium article:

https://medium.com/@farzadbagheri33/net-11-is-here-whats-new-in-c-15-asp-net-core-performance-and-ai-agents-17e570efdd48

And for the next hands-on .NET episode, I’m building a small project combining:

.NET 11 + C# 15 Union Types + Minimal API + MCP Server 

with the code explained from architecture to implementation.

If that sounds useful, follow along.

What do you think about native union types in C#?

Would you use them for API/service results, or are you sticking with your existing Result<T> / OneOf approach?