Urgent.News

What's breaking now, across thousands of outlets.

Tech

Microsoft devs rejoice: Union types coming to C# in November

The unsafe keyword will do more heavy lifting, while union types unify disparate forms of data

Microsoft devs rejoice: Union types coming to C# in November

Microsoft developers are rejoicing as the company prepares to introduce union types in the upcoming C# 15 language release in November. Union types allow different custom data types to be grouped under a single umbrella type, providing a more flexible and compiler-verified contract compared to traditional hierarchies. This feature enables better pattern matching, custom functions, protection against rogue data types, stronger abstractions, and less boilerplate error code.

Microsoft lead designer Mads Torgersen described union types as "how unions should be," praising their ability to combine custom data types like "Dog," "Cat," and "Bird" into a single "Pet" type. The introduction of union types comes alongside the addition of closed hierarchies, which restrict direct inheritance in base classes to only derived types defined within the same assembly.

This control measure prevents third-party subtypes from being compiled and spawning errors. Additionally, C# 15 expands the scope of the "unsafe" keyword, allowing developers to flag unsafe operations and maintain safety obligations, similar to Rust and Swift. Other new features in C# 15 include collection expression arguments, indexer declarations in extension blocks, and labels for break and continue statements in nested loops.

Despite Redmond's shifting priorities towards cloud subscriptions and AI Copilot, Microsoft continues to update C#, now in its 20th year, to stay relevant in the ever-evolving programming landscape.

Written by urgent.news from The Register's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Also reported by 1 other outlet

Read the original at theregister.com →

More in Tech

More from Wednesday 2 September →