User Experience

What's new in C# 13

15 min read

  • June 16, 2024
  • Rajesh Goel

C# 13 is supported on .NET 9. For more information, see C# language versioning.

New features are added to the "What's new in C#" page when they're available in public preview releases. The working set section of the roslyn feature status page tracks when upcoming features are merged into the main branch.

params collections

The params modifier isn't limited to array types. You can now use params with any recognized collection type, including System.Span, System.ReadOnlySpan, and types that implement System.Collections.Generic.IEnumerable and have an Add method. In addition to concrete types, the interfaces System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.Generic.ICollection, and System.Collections.Generic.IList can also be used. 

When an interface type is used, the compiler synthesizes the storage for the arguments supplied.

New lock object

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

The C# lock statement recognizes if the target of the lock is a Lock object. If so, it uses the updated API, rather than the traditional API using System.Threading.Monitor. The compiler also recognizes if you convert a Lock object to another type and the Monitor based code would be generated. You can read more in the feature specification for the new lock object.

New escape sequence

You can use \e as a character literal escape sequence for the ESCAPE character, Unicode U+001B. Previously, you used \u001b or \x1b. Using \x1b wasn't recommended because if the next characters following 1b were valid hexadecimal digits, those characters became part of the escape sequence.