TL;DR
Microsoft has announced support for union types in C# 15, part of the .NET 11 preview. This feature allows types to represent multiple unrelated data structures, improving pattern matching and type safety. The support is available in preview builds and may evolve before the final release.
Microsoft has officially added support for union types in C# 15, part of the upcoming .NET 11 release, enabling developers to define types that can hold multiple unrelated data structures. This development is a long-requested feature that enhances pattern matching and type safety in C#.
The support for union types was introduced in the .NET 11 preview 4, allowing developers to declare a union type with the union keyword, listing multiple types that a variable can represent. For example, public union SupportedOS(Windows, Linux, MacOS); allows an instance of SupportedOS to hold any of those three types.
Union types in C# 15 generate objects that implement the IUnion interface, providing access to the underlying value via the Value property. Pattern matching with switch expressions is the recommended way to work with these types, ensuring compile-time exhaustiveness checks. For instance, a switch expression can distinguish between Windows, Linux, and MacOS cases, with the compiler warning if any case is unhandled.
Why It Matters
This feature introduces a powerful new way to handle data that can be of multiple types, improving code safety and clarity. It simplifies scenarios where multiple unrelated types need to be processed uniformly, such as handling different operating system records or result/error types, reducing reliance on base classes or type casting.
Developers working with pattern matching, functional programming patterns, or complex data models will find union types especially beneficial, potentially influencing future language features and library design in the .NET ecosystem.
C# union types programming book
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 like F#, Rust, and TypeScript, but have long been absent from C#. Their introduction in C# 15 aligns with a broader trend toward more expressive and type-safe language features. Prior to this, handling multiple types often required cumbersome workarounds such as base classes, object references, or enum tags, which could compromise type safety or clarity.
The feature was first previewed in .NET 11 preview 2 and refined in preview 4, with developers encouraged to test and provide feedback before the final release. The addition reflects ongoing efforts to modernize C# and make it more expressive for diverse programming paradigms.
“Union types in C# 15 provide a new, type-safe way to handle data that can be one of several unrelated types, simplifying pattern matching and reducing boilerplate.”
— Microsoft Developer Blog
“The introduction of union types aligns C# more closely with functional programming languages, expanding its versatility and safety.”
— C# Language Design Team
.NET 11 preview development tools
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What Remains Unclear
It remains unclear how widely supported or adopted union types will be before the final release, as the feature is still in preview. There may be changes to syntax or implementation details based on developer feedback. Additionally, comprehensive tooling support and integration with existing libraries are yet to be fully tested and documented.
C# pattern matching guide
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
What’s Next
Developers are encouraged to experiment with union types in the current preview builds, provide feedback to Microsoft, and monitor upcoming preview releases. The final version of .NET 11 and C# 15 is expected later this year, with official documentation and broader tooling support to follow.
type-safe programming books
As an affiliate, we earn on qualifying purchases.
As an affiliate, we earn on qualifying purchases.
Key Questions
What are union types in C# 15?
Union types allow a variable to hold one of multiple specified types, enabling more flexible and type-safe handling of data structures that can vary in form.
How do I use union types in my code?
To use union types, you declare a union with the union keyword, list the types, and then instantiate or assign values accordingly. Pattern matching with switch expressions is the recommended approach to work with these types.
Are union types stable enough for production use?
Union types are currently in preview and subject to change. They are intended for testing and feedback purposes before the final release of .NET 11.
Will union types replace other patterns like inheritance or object casting?
Union types aim to simplify handling multiple data types without relying on inheritance hierarchies or unsafe casting, improving safety and clarity in many scenarios.
Source: Hacker News