Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท .NET, Tools and Concepts

What is new in C# 13

A feature tour of C# 13 on .NET 9: params collections, the System.Threading.Lock type, the \e escape, partial properties, overload priority and allows ref struct.

C# 13 shipped in November 2024 with .NET 9. Its changes are smaller than C# 12's, but several of them matter for performance: params now works with spans and other collection types, and there is a dedicated lock type that is cheaper and clearer than locking on an arbitrary object.

All examples compile with the compiler used on this site. In your own project, target net9.0 or newer, or set <LangVersion>13</LangVersion>. Features that need new library types (for example Lock) also need a runtime that has them.

C#
static int Sum(params ReadOnlySpan<int> values) { /* ... */ }

private readonly Lock _gate = new();
lock (_gate) { /* critical section */ }

Try it

C# params collections and the new Lock type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# The \e escape, index from end in initializers, partial properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Feature tour

params collections

Until C# 12, params only worked with arrays. C# 13 accepts any of these types as a params parameter: arrays, Span<T>, ReadOnlySpan<T>, and types that support collection expressions, such as List<T>, IEnumerable<T>, IReadOnlyList<T> and ICollection<T>. With the span types the compiler can place the arguments on the stack, so calling Sum(1, 2, 3) allocates nothing. The standard library has overloads such as string.Join, Path.Combine and Task.WhenAll that now take params ReadOnlySpan<T>.

System.Threading.Lock

lock (x) on any object uses a monitor, which is cheap but stores its state in the object header. The new Lock class has the same mutual-exclusion semantics with a better implementation. When the compiler sees lock (e) and e has type Lock, it calls EnterScope() and disposes the returned scope, rather than using Monitor.Enter. Locking on a Lock that you cast to object falls back to the monitor, which silently loses the benefit, so keep the field typed Lock. See lock.

The \e escape

"\e" is the escape character U+001B. Before, you wrote \u001b or \x1b, and \x1b was dangerous because \x takes up to four hex digits and could swallow the next character.

Index from the end in initializers

Object initializers may use an implicit indexer with ^: { [^1] = 100 } assigns the last element. This requires the target to have a Length or Count and an int indexer, and the usual indexer rules (see Index and Range).

Partial properties and indexers

partial members now include properties and indexers. One part declares the signature, the other implements it. This mostly helps source generators, which can generate the implementation of a property you declared, or the reverse. See partial.

Overload resolution priority

[OverloadResolutionPriority(n)] on an overload makes the compiler prefer it over others when both apply and the priority is higher. Library authors use it to steer callers toward a faster new overload without breaking existing code.

ref structs, async and iterators

  • ref struct types can now implement interfaces, and generic type parameters may be declared where T : allows ref struct, so generic code can accept Span<T>.
  • Methods that are async or iterators can declare ref locals and use ref struct locals, as long as those locals are not used across an await or yield.
  • The field keyword and the first-class span work arrived as previews in C# 13 and became final in C# 14.

Usage notes

  • Use a params span for hot paths that are called with many small argument lists. Use IEnumerable<T> or an array when callers need to pass an existing collection.
  • Lock is reentrant on the same thread (like a monitor), and you still cannot await inside a lock statement.
  • \e in verbatim and raw strings is just two characters. The escape works only in regular and interpolated strings.
  • A partial property must have exactly one declaring and one implementing part, and the two signatures must match.
  • New runtime types: Lock and the span-based BCL overloads exist only on .NET 9 and later.