AIThis post was created with the assistance of artificial intelligence (AI).

TL;DR

Before you orderOffer from Amazon

Get the latest gadgets delivered free with Prime

  • Fast, free delivery on millions of items
  • Prime Video, Amazon Music and more included
  • Member-only deals all year
Start your free Prime trial Free trial for eligible customers · Cancel anytime
As an affiliate, we earn on qualifying purchases.

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

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

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

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.

Amazon

C# pattern matching guide

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.

Amazon

functional programming C# books

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 <LangVersion>preview</LangVersion> 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

FALL

Fall Picks

As an affiliate, we earn on qualifying purchases.

You May Also Like

The $725 Billion Question: Hyperscaler Capex Q1 2026 and What the Earnings Don’t Answer

The Big Four hyperscalers announced a combined $725 billion in AI-related capital expenditure for 2026, raising questions about future revenue growth and industry sustainability.

Working around dragons with the Lemote Yeeloong laptop and OpenBSD

Exploring how enthusiasts run OpenBSD on the Lemote Yeeloong laptop powered by a Chinese MIPS64 chip, highlighting challenges and significance.

Bloom Energy (BE) Valuation In Focus After AI Data Center Deals And Upgraded 2026 Outlook

Bloom Energy’s valuation gains attention following new AI data center contracts and an upgraded 2026 financial outlook, raising investor interest.

Anthropic confidentially submits draft S-1 to the SEC

Anthropic has confidentially submitted a draft S-1 to the SEC, signaling potential plans to go public, contingent on market conditions.