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.
