What's new in C# 13
C# 13 includes the following new features. You can try these features using the latest Visual Studio 2022 version or the .NET 9 Preview SDK.
- New escape sequence -
\e
. - Method group natural type improvements
- Implicit indexer access in object initializers
C# 13 is supported on .NET 9. For more information, see C# language versioning.
You can download the latest .NET 9 preview SDK from the .NET downloads page. You can also download Visual Studio 2022 - preview, which includes the .NET 9 Preview SDK.
New features are added to the "What's new in C#" page when they are 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.
Note
We're interested in your feedback on these features. If you find issues with any of these new features, create a new issue in the dotnet/roslyn repository.
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.
Method group natural type
This feature makes small optimizations to overload resolution involving method groups. The previous behavior was for the compiler to construct the full set of candidate methods for a method group. If a natural type was needed, the natural type was determined from the full set of candidate methods.
The new behavior is to prune the set of candidate methods at each scope, removing those candidate methods that aren't applicable. Typically, these are generic methods with the wrong arity, or constraints that aren't satisfied. The process continues to the next outer scope only if no candidate methods have been found. This process more closely follows the general algorithm for overload resolution. If all candidate methods found at a given scope don't match, the method group doesn't have a natural type.
You can read the details of the changes in the proposal specification.
Implicit index access
The implicit "from the end" index operator, ^
, is now allowed in an object initializer expression. For example, you can now initialize an array in an object initializer as shown in the following code:
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 can't be used in an object initializer. You need to index the elements from the front.
See also
Phản hồi
https://aka.ms/ContentUserFeedback.
Sắp ra mắt: Trong năm 2024, chúng tôi sẽ dần gỡ bỏ Sự cố với GitHub dưới dạng cơ chế phản hồi cho nội dung và thay thế bằng hệ thống phản hồi mới. Để biết thêm thông tin, hãy xem:Gửi và xem ý kiến phản hồi dành cho