New features in C# 13

The release of C# 13 brings with it some exciting new features. These features can be explored using the latest version of Visual Studio 2022 or the .NET 9 Preview SDK.

New escape sequence – e

The new escape sequence e can be used as a character literal for the Unicode ESCAPE character U+001B. Previously, u001b or x1b were used. The use of x1b was not recommended because, if the characters following 1b were valid hexadecimal digits, they would become part of the escape sequence.

Improvements in natural types of method groups

This function optimizes overload resolution for method groups. Previously, the compiler generated the complete set of candidate methods for a method group. If a natural type was required, the natural type was determined from the complete set of candidate methods.

The new behavior involves narrowing down the set of candidate methods in each region by removing inapplicable ones. These are typically generic methods with the wrong arity or conditions that are not met. The process continues to the next outer region only if no suitable candidate methods are found. This process more closely follows the general overload resolution algorithm. If all candidate methods in a given region do not match, the method group has no natural type.

Implicit index access

The implicit "from end" index operator ^ is now allowed in an object initialization expression. For example, you can now initialize an array in an object initialization like this:

"'

var v = new S()

{

buffer =

{

[^1] = 0,

[^2] = 1,

[^3] = 2,

[^4] = 3,

[^5] = 4,

[^6] = 5,

[^7] = 6,

[^8] = 7,

[^9] = 8,

[^10] = 9,

}

}

”’;

In versions prior to C# 13, the ^ operator could not be used in object initialization. You had to index the elements from the beginning.

C# 13 is supported by .NET 9. For more information, see [link to documentation]. C# language versioning.

Source: What's New in C# 13 – C# Guide – C# | Microsoft Learn