As the annual release cycle for the .NET ecosystem approaches, Microsoft has provided a comprehensive look at the upcoming features in C# 15. This latest iteration of the long-standing programming language continues a multi-year trend of blending high-level functional programming paradigms with low-level performance optimizations. The development team at Microsoft has focused on addressing long-standing developer requests, particularly regarding type safety, code expressiveness, and more granular control over memory management.
The announcement marks a significant milestone for C#, which remains one of the most popular languages for enterprise development, cloud-native applications, and game development. By introducing features such as union types and closed hierarchies, Microsoft is narrowing the gap between C# and languages like F#, Rust, and Swift, which have long championed these structural patterns.
The Evolution of Type Safety: Union Types
Perhaps the most significant addition to C# 15 is the introduction of union types. For years, C# developers have relied on inheritance or interfaces to group different types under a single umbrella. However, these traditional methods often require a shared base class or a common interface, which may not always be architecturally sound or even possible—especially when dealing with primitive types or classes from third-party libraries.
A union type in C# 15 is a structure that explicitly defines a fixed set of supported types. Unlike a standard class hierarchy, a union does not require its members to share any common lineage. For example, a developer can now define a type that is specifically an integer, a version object, or a collection of strings, without forcing these disparate types into a shared interface.
The practical benefits of this change are most evident in API design. Historically, handling a variety of potential return types—such as a successful data object, a validation error, or a system failure—required complex class hierarchies or the use of the object type. Using object necessitates frequent casting and manual type checking, which is notoriously error-prone and often bypasses the compiler’s ability to ensure safety.
With union types, the compiler gains full visibility into the possible states of a variable. This enables "exhaustive switching," a feature where the compiler can verify at build time that every possible type in a union is handled by a switch expression. If a developer adds a new type to the union but forgets to update a corresponding switch block, the compiler will issue a warning. This eliminates the need for a "default" catch-all branch, which has historically been a source of silent bugs when new subclasses were introduced to a system without proper handling logic.
Furthermore, union types allow for the implementation of custom methods and properties directly within the union declaration. This enables developers to encapsulate logic that applies to the entire set of types, facilitating patterns like monads and improving the readability of complex business logic.
Architectural Control Through Closed Hierarchies
Building on the theme of restricted type systems, C# 15 introduces "closed hierarchies." This feature is designed to give library authors and framework architects more control over how their code is extended by external consumers.
A closed hierarchy prevents a class from being inherited by any code outside the assembly in which it was declared. This behavior essentially combines the visibility constraints of the internal modifier with the inheritance restrictions of the sealed keyword. While a sealed class cannot be inherited at all, a closed class allows for inheritance within its own project but blocks it for any external developers who reference the library.
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.

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.
In earlier versions of the language, initializing a List<T> with a specific capacity or a HashSet<T> with a specific string comparer required multiple lines of code or a break in the clean "collection expression" syntax ([...]). C# 15 introduces the with() keyword inside collection expressions to bridge this gap.
By using with(), developers can pass arguments directly to the collection’s constructor while still using the concise bracket syntax for the initial data. For instance, a developer can now initialize a list, set its capacity, and populate it with values in a single, readable line. Microsoft notes that the with keyword must appear as the first element in the expression to ensure the compiler can correctly identify the intended constructor before the collection is populated.
Redefining Memory Safety Rules
Finally, C# 15 introduces a significant shift in how the language handles memory safety. As industry-wide concerns regarding memory-safe programming have grown, Microsoft has sought to refine what C# considers "unsafe."
The traditional approach in C# required the unsafe keyword for any code block that utilized pointers. However, the language team has determined that the mere existence of a pointer is not inherently dangerous; the danger arises only when a pointer is dereferenced or used to access unmanaged memory.
In the new version, the requirement for the unsafe keyword has been narrowed. Developers can now declare and pass pointers in "safe" contexts, with the unsafe requirement only triggering at the point of memory access. Additionally, a new safe keyword has been introduced to mark blocks of code where the developer has manually verified that an operation is safe for the caller, even if the compiler cannot verify it automatically. This is particularly relevant for interop scenarios involving extern methods or structs with explicit memory layouts.
These memory safety changes are intended to encourage better performance tuning without the "all-or-nothing" stigma previously associated with unsafe code. However, because these rules represent a change in compiler behavior, they must be explicitly enabled by the developer in the project settings.
Conclusion
The features revealed for C# 15 demonstrate a language that is maturing but remains remarkably agile. By incorporating functional programming strengths like union types alongside low-level refinements like updated memory safety rules, Microsoft is ensuring that C# remains a viable choice for a vast range of programming tasks.
While some features, such as labeled breaks or collection arguments, focus on reducing daily friction, others, like closed hierarchies and union types, provide the tools necessary for building more robust and maintainable large-scale systems. As the .NET community prepares for the official release, these changes suggest a future for C# that is more expressive, more secure, and increasingly tailored to the needs of modern software architecture.
