TL;DR
Microsoft announced that .NET 11 preview 4 includes support for union types in C# 15, a feature long-requested by developers. This allows types to represent multiple possible data variants, improving code safety and expressiveness.
Microsoft has introduced support for union types in C# 15, available in the latest .NET 11 preview 4. This feature allows developers to define types that can represent multiple, distinct data variants, addressing long-standing requests for more expressive type handling in C#.
Union types in C# 15 are enabled via the new union keyword, allowing developers to create types that can hold one of several specified types. For example, a SupportedOS union type can encompass Windows, Linux, or MacOS types, with pattern matching and switch expressions simplifying their handling. Meta is in crisis, Google Search’s makeover, and AI gets booed by graduates.
Developers can instantiate union types directly or through implicit conversions, and access the contained value via the Value property. The compiler enforces exhaustive handling, warning if any union cases are unhandled, thus improving safety and reducing runtime errors.
Why It Matters
The addition of union types in C# significantly enhances the language’s capacity for functional programming patterns, such as option types and result types, making code more expressive and safer. It addresses a major gap in C#’s type system that has been present since its inception, aligning C# more closely with functional languages like F# and Rust.
This development could influence how developers design APIs, especially those involving complex data variants, error handling, or state management, potentially leading to more robust and maintainable software. Who gets to own the Luigi Mangione story?.

The C Programming Language
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Background
Union types are common in functional programming languages and are used to represent data that can take multiple forms. Prior to this update, C# developers relied on base classes, object types, or tagging enums to simulate similar behavior, often at the expense of safety and clarity. The feature was requested for years, with community feedback influencing its inclusion in the upcoming .NET 11 release.
The support was first introduced in preview builds, with the current release in preview 4, indicating ongoing refinement before a stable release. This aligns with Microsoft’s broader strategy to modernize C# and improve support for functional programming paradigms.
“Union types in C# 15 bring a new level of expressiveness and safety, enabling developers to model data more naturally and concisely.”
— Microsoft .NET team
“This feature aligns C# more closely with functional languages, making pattern matching and data handling more robust.”
— C# language designer

C# 15 and .NET 11 Programming Preview: Performance enhancements in .NET 11 through runtime-level async optimization and JIT improvements (The Great Minds)
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What Remains Unclear
While union types are now available in preview 4, it is not yet clear how they will be fully integrated into the final release, including potential syntax changes or additional features. The community’s feedback may influence further adjustments before the official launch.

C# in Depth: Fourth Edition
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What’s Next
Microsoft is expected to continue refining union types based on community feedback, with a stable release anticipated in the next major version of .NET. Developers are encouraged to experiment with the preview and provide input.

C# PROGRAMMING FOR GUI DEVELOPMENT: Learn to Create Visually Appealing and Functional Software Experiences (C# Vanguard Series)
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Key Questions
How do I enable union types in my C# project?
You need to install .NET 11 preview 4 or later and set in your project file, then use the union keyword to define union types.
Can I use union types with existing C# types?
Union types are a new language feature and are designed to work alongside existing types. You can define unions that include custom types, but they are not compatible with all existing types automatically.
Will union types replace other pattern matching techniques?
Union types will augment pattern matching capabilities, providing a more natural way to handle data variants, but traditional techniques like base classes or enums will still be useful in many scenarios.
Are there any limitations or known issues with union types in the preview?
As a preview feature, union types may have bugs or incomplete functionality. The final version may include additional features or changes based on developer feedback.
Source: Hacker News