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.
params
collections- New
lock
type and semantics. - 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'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.
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.
params
collections
The params
modifier isn't limited to array types. You can now use params
with any recognized collection type, including System.Span<T>, System.ReadOnlySpan<T>, and types that implement System.Collections.Generic.IEnumerable<T> and have an Add
method. In addition to concrete types, the interfaces System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.Generic.ICollection<T>, and System.Collections.Generic.IList<T> can also be used.
When an interface type is used, the compiler synthesizes the storage for the arguments supplied. You can learn more in the feature specification for params
collections.
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.
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, the removed methods 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 are 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 countdown = new TimerRemaining()
{
buffer =
{
[^1] = 0,
[^2] = 1,
[^3] = 2,
[^4] = 3,
[^5] = 4,
[^6] = 5,
[^7] = 6,
[^8] = 7,
[^9] = 8,
[^10] = 9
}
};
The preceding example creates an array that counts down from 9 to 0. In versions before C# 13, the ^
operator can't be used in an object initializer. You need to index the elements from the front.
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for