C# 15 preview adds union types ahead of November release
Microsoft is preparing to launch C# 15 in November, with a preview build already showing its most heavily requested feature: union types. These let developers group several distinct custom data types under a single umbrella type, giving the compiler a fixed, verifiable list of permitted variants and enabling simpler pattern matching, custom functions for grouped types, and less boilerplate error-handling code. C# lead designer Mads Torgersen has championed the feature, arguing it lets developers express designs that traditional class hierarchies cannot.
The update also introduces "closed hierarchies," which restrict inheritance of a base class to derived types within the same assembly, blocking third-party subtypes and reducing compile errors. Separately, the unsafe keyword's role is being expanded beyond flagging risky memory operations to enforcing safety obligations on callers, echoing stricter approaches used in Rust and Swift. Other additions include collection expression arguments, indexers within extension blocks, and labelled break/continue statements for nested loops. The release continues the development of C#, created by Anders Hejlsberg and first launched by Microsoft in 2002, even as the company's broader focus has shifted towards cloud subscriptions and AI tools.
- C# 15 arrives in November with new union types feature
- Unions let developers group custom types under one umbrella type
- Unsafe keyword's scope expands, following Rust and Swift's approach
Read the full article at the source →
Originally published by The Register as “Microsoft devs rejoice: Union types coming to C# in November”.