.NET 11 Preview 3 brings exciting new features to C# 14, including union type support - a long-awaited feature that allows developers to define types that can hold one of several distinct values.
What are Union Types?
Union types (also known as tagged unions or discriminated unions) allow you to define a type that can be one of several different types. This is incredibly useful for domain modeling, error handling, and creating more expressive APIs.
Example
// Define a union type using the new 'as' keyword
public as Result<T> = Success<T> | Failure;
public record Success<T>(T Value);
public record Failure(string ErrorMessage);
Benefits
- Type Safety: The compiler knows all possible variants
- Exhaustiveness: You must handle all cases in switch expressions
- Domain Modeling: Perfect for modeling business rules
- Clean Code: No more null checks or ambiguous return types
This blog post explores how union types work, their benefits for domain modeling, and practical examples of how they can simplify C# codebases.

Comments (0)
Please sign in to leave a comment.