DEV Community

Vikrant Bagal
Vikrant Bagal

Posted on

C15 Union Types in .NET 11: Say Goodbye to Type Unwrapping

C# 15 Union Types in .NET 11: Say Goodbye to Type Unwrapping

If you've been waiting for one of the most requested C# features, it's finally here. Microsoft introduced Union Types in C# 15, part of the upcoming .NET 11 SDK currently in Preview 2.

What Are Union Types?

A union type allows a variable to hold one value from a closed set of multiple types. Think of it as a compile-time safer alternative to object or dynamic when you specifically need a type that could be A, B, or C.

The Old Way

Before union types, we had to deal with type unions like this:

// Painful type checking
object value = GetValue();

if (value is string str)
{
    Console.WriteLine(str.ToUpper());
}
else if (value is int num)
{
    Console.WriteLine(num * 2);
}
else if (value is DateTime date)
{
    Console.WriteLine(date.ToShortDateString());
}
Enter fullscreen mode Exit fullscreen mode

The Union Type Way

With union types in C# 15, it becomes elegantly simple:

// Declare a union type
ValueWithUnion<string | int | DateTime> result = GetValue();

// Type-aware access
switch (result)
{
    case string str:
        Console.WriteLine(str.ToUpper());
        break;
    case int num:
        Console.WriteLine(num * 2);
        break;
    case DateTime date:
        Console.WriteLine(date.ToShortDateString());
        break;
}
Enter fullscreen mode Exit fullscreen mode

Why This Matters

1. Type Safety Without Compromise

Union types give you the flexibility of multiple types without sacrificing type safety. The compiler ensures you handle all possible cases.

2. Better Self-Documentation

Code like string | int | DateTime immediately tells other developers what types are possible. No need to read through lengthy comments or documentation.

3. Reduced Runtime Errors

Since all union cases must be explicitly handled, you eliminate whole classes of runtime errors that come from unhandled type scenarios.

Practical Use Cases

API Response Handling

// Parse response that could be string, int, or JSON object
Result<string | int | Dictionary<string, object>> result = 
    ParseApiResponse(response);
Enter fullscreen mode Exit fullscreen mode

Validation Results

// Validation can return error message, error code, or detailed errors
Result<string | int | ValidationError[]> validation = Validate(input);
Enter fullscreen mode Exit fullscreen mode

Cache Responses

// Cached data might be any of several types
ValueWithUnion<string | DateTime | int> cachedItem = cache.Get(key);
Enter fullscreen mode Exit fullscreen mode

How to Try It Today

Union types are part of C# 15, which ships with .NET 11 Preview 2. You can get started by:

  1. Installing the .NET 11 Preview 2 SDK
  2. Using Visual Studio 2026 Insider or later
  3. Creating a new project with <TargetFramework>net11.0</TargetFramework>

What's Next?

Union types are just one of the exciting features in C# 15. Other notable additions include collection expression arguments and several performance improvements.

The .NET 11 SDK is expected to reach General Availability in November 2026. Keep an eye on the .NET Blog for the latest updates.


Enjoyed this article? Let's connect on LinkedIn: https://www.linkedin.com/in/vikrant-bagal/

Check out my portfolio for more .NET content: https://vrbagalcnd.github.io/portfolio-site

Top comments (0)