.NET (OK, C#) gets union types

TL;DR

Microsoft has announced that .NET 11’s C# 15 will include native support for union types, a feature long requested by developers. This allows variables to hold one of several types, improving type safety and code clarity. The feature is currently in preview and may change before the final release.

Microsoft has officially announced that .NET 11, through its C# 15 language update, will support union types, a feature long requested by developers for safer multi-type handling. This addition allows C# programmers to define variables that can hold one of several specified types, simplifying code and reducing errors. The feature is currently available in preview and is expected to be included in the final release of .NET 11.

Union types in C# 15 are introduced via the ‘union’ keyword, allowing developers to declare a variable that can contain one of multiple types, such as different record types or basic data types. For example, one can define a union type ‘SupportedOS’ that can hold either a Windows, Linux, or MacOS record, and then instantiate or switch between these types seamlessly.

The support is available in .NET 11 preview 4, requiring developers to enable preview language features in their project files. The implementation generates a union type that implements the IUnion interface, providing access to the contained value via a ‘Value’ property. Pattern matching with switch expressions is the recommended way to work with union types, as it enforces exhaustive handling of all possible contained types, reducing runtime errors.

Why It Matters

This development represents a major enhancement in C#’s type system, bringing it closer to functional programming languages that natively support union types. It simplifies handling data that can be of multiple unrelated types, improving code safety, readability, and maintainability. For developers working with complex data models or interoperability scenarios, this feature reduces the need for cumbersome workarounds like base classes or type casting.

Moreover, the inclusion of union types in a mainstream language like C# could influence other languages and frameworks to adopt similar features, fostering a broader shift towards more expressive type systems in the .NET ecosystem and beyond.

Amazon

C# 15 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 languages such as F#, Rust, and TypeScript, but have been absent in C# until now. Developers have relied on patterns like inheritance, object wrappers, or enums with type tags to simulate similar behavior, often at the cost of verbosity or safety. To understand more about modern financial platforms, visit Robinhood’s recent strategies. The feature’s inclusion in C# 15 is a response to longstanding community requests, as evidenced by discussions on developer forums and proposals over the past years.

The support was first introduced in preview in .NET 11, with initial experiments in preview 2, and refined in preview 4. For more insights on data privacy, see Oura’s data privacy updates. The feature aligns with Microsoft’s ongoing efforts to modernize C# and make it more expressive for diverse programming paradigms.

“The addition of union types in C# 15 marks a significant step towards more expressive and type-safe code, enabling developers to handle multiple data types cleanly.”

— Microsoft C# team member

“Union types will simplify many patterns we’ve been using, like handling optional or variant data, making code more straightforward and less error-prone.”

— Developer community member

Amazon

.NET 11 development tools

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 the implementation will evolve before the final release, and whether all planned features, such as more advanced pattern matching or integration with existing type features, will be included. Additionally, the full impact on existing codebases remains to be seen once the feature is more widely adopted.

Amazon

C# pattern matching switch expressions

As an affiliate, we earn on qualifying purchases.

As an affiliate, we earn on qualifying purchases.

What’s Next

Microsoft plans to continue refining union types during the preview phase, with feedback from developers guiding the final implementation. The final version of .NET 11, expected later this year, will include stable support for union types. Developers are encouraged to experiment with the preview and provide feedback to influence the feature’s final form.

Following release, tools, libraries, and frameworks will likely update to leverage this feature, and tutorials or best practices will emerge to help developers adopt union types effectively.

Amazon

programming language support for union types

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?

To use union types, you need to install the .NET 11 preview SDK and enable preview language features by adding preview in your project file’s .

Can I use union types with existing data types or only custom types?

Union types can be used with any types supported in C#, including built-in types like int, string, or custom record types. You define a union over the specific types you need.

Will union types replace other patterns like inheritance or enums?

Union types provide an alternative to inheritance and tagged unions, offering a more direct and type-safe way to handle multiple types. They are not expected to fully replace existing patterns but will complement them, especially in scenarios requiring variant data handling.

Are there any performance impacts of using union types?

Performance details are still being evaluated, but initial implementations suggest minimal overhead, similar to pattern matching and type casting. Keep up with the latest developments in the industry at Robinhood’s recent updates. Further optimizations may be introduced before the final release.

Source: Hacker News

You May Also Like

Hyperpolyglot Lisp: Common Lisp, Racket, Clojure, Emacs Lisp

A detailed look at a developer proficient in Common Lisp, Racket, Clojure, and Emacs Lisp, exploring their skills, tools, and significance in programming.

From Prompt to Profit: How AI Form Builders Rapidly Build Your Funnel

Discover how AI form builders turn simple prompts into complete lead funnels in seconds. Save time, boost conversions, and automate your marketing today.

Cordless Drill Buyer’s Guide: Voltage, Torque, and Battery Ecosystems

What factors like voltage, torque, and battery ecosystems should you consider to choose the perfect cordless drill for your needs?

Home Workshop Table Saw Guide: Fence Accuracy, Safety, and Dust Collection

Learn essential tips for optimizing your table saw with precise fence setup, safety features, and dust collection to ensure a safer, cleaner workshop—discover more now.