C# 15 Preview Adds Union Types, Closed Hierarchies, and Expanded Unsafe Keyword
Microsoft's C# 15 preview introduces union types for compiler-verified type combinations, closed hierarchies to restrict inheritance, an expanded unsafe keyword for stricter memory safety, plus collection expression parameters, indexers in extensions, and labeled break/continue statements, all shipping with .NET 11 in November.
Microsoft plans to release the next generation of the C# language in November 2025. The recently published C# 15 preview introduces stricter data-type constraints and enhanced memory-safety protections. The most anticipated feature is union types, a concept already present in Python, Scala, and TypeScript but implemented in C# with a distinct design philosophy.
Union Types: Compiler-Verified Type Combinations
C# union types provide a way to group different custom data types into a single entity. The union keyword merges a set of custom types — for example, Cat, Dog, and Bird — into one umbrella type such as Pet. The compiler receives an explicit, closed list of permitted types that cannot be extended.
public record class Cat(string Name);
public record class Dog(string Name);
public record class Bird(string Name);
public union Pet(Cat, Dog, Bird);
Pet pet = new Dog("Rex");
string name = pet switch
{
Dog d => d.Name,
Cat c => c.Name,
Bird b => b.Name, // omit one arm and the compiler complains
};According to Microsoft documentation, “Unions enable designs that traditional hierarchies cannot express, combining arbitrary combinations of existing types into a single, compiler-verified contract.” Benefits for developers include better pattern matching, the ability to write custom functions for the union type, prevention of unintended types, stronger abstractions, and less boilerplate code.
Practical Demonstration: Success/Failure Union
In a presentation, C# chief designer Mads Torgersen and principal engineer Dustin Campbell showed how unions can merge two distinct record types to represent the success or failure of a network input stream. One record uses an integer for success, the other a standard text error message. Combining them with union allows a single switch expression to parse the result.
Without this shortcut, developers would have to add error messages in switch statements to handle unknown input, create a wrapper (which consumes more memory and is more error-prone), or deploy a third-party library to parse the result.
Closed Hierarchies: Restricting Inheritance
While union types limit allowed types, C# 15 also introduces closed hierarchies for inheritance scenarios. A base class marked with the closed keyword restricts direct inheritance to derived types defined in the same assembly. This prevents third-party subtypes from being compiled, which would otherwise likely cause errors.
Expanded unsafe Keyword: Propagating Safety Obligations
The unsafe keyword originally only warned the compiler that a code block might interact with memory in unsafe ways. Now its scope has expanded to “inform callers that they have an obligation to uphold these obligations to maintain safety.” The extended keyword scope enables the compiler to flag unsafe operations. Microsoft product manager Richard Lander noted this change follows the stricter, propagation-oriented semantics used in Rust and Swift.
Additional C# 15 Features
Collection expression parameters — developers can use the with keyword to pass arguments (e.g., capacity or comparer) directly to the underlying collection constructor within collection expression syntax.
Indexers in extension blocks — indexers can now be declared inside extension blocks.
Labeled break and continue — labels can be added to these statements for finer control flow through nested loops.
Historical Context and Availability
C# was originally created by Anders Hejlsberg and first released by Microsoft in 2002 as a Java alternative for the new .NET platform on Windows. Over two decades, the language has pioneered features such as asynchronous programming and the LINQ query interface. Despite Microsoft’s shift toward cloud subscriptions and AI assistants like Copilot, C# continues to receive regular updates.
To try C# 15, download the .NET 11 preview .
Reference: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-15
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
