November 23, 2026

Microsoft Unveils C# 15: A Deep Dive into Union Types, Enhanced Memory Safety, and Language Refinements

This is particularly useful for maintaining "binary compatibility" and preventing the "fragile base class" problem. When a developer makes a class public, they often inadvertently commit to supporting any number of unknown external implementations. By marking a hierarchy as closed, the author ensures that they know exactly how many subclasses exist, allowing them to optimize performance and change internal logic without fear of breaking external code.

Architecturally, every class marked as closed is implicitly abstract, ensuring it serves only as a blueprint for internal implementations. It is important to note that this restriction applies only to classes and record classes; structs, which do not support inheritance, and interfaces, which are designed specifically for external implementation, cannot be marked as closed.

Refined Flow Control: Labeled Break and Continue

While much of the C# 15 update focuses on high-level architecture, the Microsoft team has also introduced quality-of-life improvements for everyday coding tasks. One such addition is the implementation of labeled break and continue statements.

In previous versions of C#, interrupting the execution of a nested loop or a switch statement inside a loop often required the use of "flag" variables—boolean markers that the outer loop would check after the inner block finished. While functional, this pattern often led to "boilerplate" code that obscured the developer’s intent. Alternatively, some developers used the goto statement, though this is generally discouraged in modern software engineering due to its potential to create "spaghetti code" where execution jumps unpredictably across a method.

What's new in C# 15: Overview

C# 15 provides a structured alternative by allowing developers to apply labels to loops and switch statements. A break or continue statement can then reference these labels to specify exactly which level of nesting should be affected. This syntax provides the precision of a goto statement while maintaining the readability and safety of structured flow control. Labels can be applied to for, foreach, while, do, and switch constructs, offering a more expressive way to handle complex iteration logic.

Expanding the Power of Extension Indexers

Extension methods have been a staple of C# for over a decade, allowing developers to "add" methods to existing types without modifying their source code. C# 15 takes this concept a step further by introducing extension indexers.

This feature allows developers to define how elements within a collection are accessed via the familiar bracket [] syntax, even if they do not have access to the original class definition. A primary use case for this is providing Range or Index support to legacy interfaces like IReadOnlyList. Previously, developers had to call specific methods like .Slice() to achieve this. Now, they can implement an extension indexer that allows the interface to be sliced using the same syntax as a standard array.

To manage potential conflicts, Microsoft has established a clear order of precedence for indexer resolution. Explicit indexers defined within the type itself always take priority, followed by implicit type indexers. Extension indexers are only utilized if no native implementation is found. This ensures that the introduction of extension indexers does not accidentally break existing code that relies on a class’s internal logic.

Streamlining Collection Initialization

Continuing the work started in C# 12 and 13, the new version introduces "collection expression arguments." This feature is aimed at reducing the verbosity of collection initialization, particularly when specific constructor arguments—such as initial capacity or custom comparers—are required.