C# 13.0 - Overview and new features What's New in C# 13.0: Language Enhancements, Advanced Features, and Deeper Integration with .NET 9

C# 13.0 - Panoramica e nuove funzionalità

Over the past few months, we had the opportunity to thoroughly test the new features of C# 13.0, officially released in November 2024 alongside ASP.NET Core 9.0. Our testing spanned real-world projects and prototypes across various contexts, including enterprise applications, cloud services, and microservices. This hands-on approach allowed us to explore the potential of this version in practical scenarios and evaluate the improvements in terms of productivity, performance, and usability.

C# 13.0 introduces a range of enhancements that simplify writing concise, readable, and high-performing code. Among the highlights are the optimized thread synchronization capabilities, improved support for params collections, and new functionality for object initializers. Furthermore, this version has been designed to further enhance integration with the .NET 9 runtime, enabling developers to leverage the full potential of an increasingly advanced and feature-rich ecosystem.

We explored features such as support for ref variables in asynchronous methods and iterators, as well as improvements to method overload resolution, which proved particularly useful for developing robust and modular APIs. The results were impressive: cleaner code, improved performance in asynchronous operations, and a smoother workflow thanks to tools that simplify managing complex scenarios.

In this article, we’ll walk you through the key innovations introduced in C# 13.0, enriched with practical examples and technical details. Whether you’re building new applications or updating existing projects, you’ll find in C# 13.0 a language equipped with new solutions to tackle the challenges of modern development.

Extended params Collections

In C# 13.0, the params modifier is no longer restricted to arrays. It can now be used with any recognized collection type, including System.Span<T>, System.ReadOnlySpan<T>, and types that implement System.Collections.Generic.IEnumerable<T> with an Add method. This provides greater flexibility in method definitions.

Example:

In this example, the Concat method accepts a variable number of parameters of type ReadOnlySpan<T>, enabling more flexible collection handling.

New Lock Type

The .NET 9 runtime introduces a new thread synchronization type: System.Threading.Lock. This type provides better synchronization through its API. The Lock.EnterScope() method enters an exclusive scope, returning a ref struct that supports the Dispose() pattern to exit the exclusive scope.

Example:

In this example, using Lock improves synchronization management compared to the traditional lock statement.

New Escape Sequence

C# 13.0 introduces the \e escape sequence to represent the ESCAPE character Unicode U+001B. Previously, developers used \u001b or \x1b, which could cause ambiguity if followed by valid hexadecimal digits.

Example:

This new escape sequence simplifies representing the ESCAPE character in strings.

Natural Type of Method Groups

This feature optimizes overload resolution involving method groups. The compiler now eliminates non-applicable methods in each scope, improving the efficiency of selecting the appropriate method.

Example:

Here, the compiler efficiently determines the natural type of the PrintMessage method group.

Implicit Index Access in Object Initializers

The "from the end" index operator (^) is now allowed in object initializer expressions. This allows initializing arrays or collections using negative indices to reference elements from the end.

Example:

In this example, the Buffer array is initialized using the ^ operator to access elements from the end.

C# 13.0 also introduces several other advanced features, such as:

  • ref and unsafe in Iterator and Async Methods: It’s now possible to declare ref local variables or use unsafe contexts in methods using yield return or async, with restrictions to ensure code safety.
  • ref struct in Generic Parameters: ref struct types can now be used as type arguments in generics, expanding API design possibilities.
  • Partial Properties and Indexers: Properties and indexers can now be declared as partial within partial types, improving code modularity.
  • Overload Resolution Priority: Library authors can designate a preferred overload using the OverloadResolutionPriorityAttribute, improving clarity in method selection.

These improvements make C# 13.0 a significant update, providing developers with more powerful and flexible tools to write efficient, modern code.

For a deeper dive into the new features, we recommend exploring the official Microsoft documentation on C# 13.0.

Useful Links

These links will help you further explore the features of C# 13.0 and the .NET 9 ecosystem. If you need additional details or resources, feel free to ask!

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.