November 23, 2026

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

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.