.NET (OK, C#) gets union types

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.

Amazon

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

Amazon

.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.

Amazon

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.

Amazon

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

You May Also Like

You can make an app for that

AI tools now enable individuals to create custom apps for their needs, transforming software development and personal productivity.

Password Manager Basics: The Setup Most People Skip

Creating a solid password manager setup is crucial, but many overlook key steps that could leave their digital life vulnerable—discover what you’re missing.

OpenAI and Malta partner to bring ChatGPT Plus to all citizens

OpenAI and Malta have announced a partnership to provide ChatGPT Plus to all Maltese citizens, marking a significant step in AI accessibility.

Yt-dlp – [Announcement] Bun support is now limited and deprecated

Yt-dlp announces limited support and deprecation of Bun JavaScript runtime, citing security concerns and development changes. Support will be restricted to specific Bun versions.