TL;DR
Microsoft has announced that .NET 11 preview 4 introduces union types support in C# 15, allowing developers to define types that can represent multiple unrelated data structures. This feature enhances type safety and expressiveness in C#.
Microsoft has announced that .NET 11 preview 4 introduces support for union types in C# 15, a feature long requested by the developer community. This addition allows C# developers to define types that can hold one of several unrelated data structures, improving code safety and clarity. This addition allows C# developers to define types that can hold one of several unrelated data structures, improving code safety and clarity.
Union types in C# 15 are enabled via the new union keyword, allowing developers to specify a type that can contain multiple distinct types. For more on language features, see this recent discussion. For example, a SupportedOS union type can include Windows, Linux, or MacOS records. Instances of these unions can be created with explicit or implicit conversions, and pattern matching with switch expressions enforces exhaustive handling of all cases.
To use these features, developers must install the .NET 11 preview SDK (preferably preview 4) and enable preview language features in their project files by setting . The support is available only in the preview phase, with potential changes before the final release.
Why It Matters
The addition of union types in C# 15 significantly enhances the language’s expressiveness, particularly for functional programming patterns, error handling, and working with data that can take multiple forms. Developers interested in how AI impacts tech trends can explore related insights here. It reduces the need for workarounds like base classes or type casting, improving code safety and readability.
This feature aligns C# more closely with other functional languages like F#, Rust, and TypeScript, which have long supported union types, and responds to longstanding developer demands for more flexible type constructs in C#.

The C Programming Language
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Background
Union types are a common feature in functional programming languages and have been requested by C# developers for years. Prior to this, handling multiple types involved complex inheritance hierarchies, type checking, or using object references, all of which had limitations and safety concerns. For broader context on data modeling, see this report. The support in C# 15 marks a major language evolution, following other modern features like pattern matching and nullable reference types.
“The introduction of union types in C# 15 provides developers with a powerful new way to model data that can take multiple forms, improving both safety and clarity.”
— Microsoft C# team member
“This feature brings C# closer to the expressive power seen in functional languages, enabling more concise and safer code.”
— Open-source contributor involved in C# language design
.NET 11 preview SDK download
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What Remains Unclear
It is not yet clear how widely adopted the feature will be in production, as it is currently available only in the preview phase. The final implementation details and potential limitations before the official release remain to be seen.

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 will likely continue refining union types based on developer feedback during the preview period. The final version is expected to be included in the official release of .NET 11, with documentation and tooling updates to support it fully.
Developers are encouraged to experiment with the preview and provide feedback to shape the final implementation.

Visual Studio Code – The Essentials: VS Code Day Preview Edition
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Key Questions
What are union types in C#?
Union types are a new feature in C# 15 that allow a type to represent one of multiple different types, enabling more flexible and type-safe handling of various data forms.
How do I enable union types support in my project?
You need to install the .NET 11 preview SDK (preferably preview 4) and set in your project file. Then, you can define union types using the union keyword.
Can I use union types with existing C# code?
Yes, but only in projects targeting the .NET 11 preview and with language support enabled. Integration with existing codebases will depend on how the new types are used and whether pattern matching can be integrated smoothly.
Will union types be available in the final release of .NET 11?
Yes, they are planned to be part of the final release, but the implementation may be refined based on developer feedback during the preview phase.
Source: Hacker News