Skip to content

Commit af2fdc4

Browse files
committed
Add feature description for ref unsafe
1 parent e1e657c commit af2fdc4

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

docs/csharp/whats-new/csharp-13.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ C# 13 includes the following new features. You can try these features using the
1313
- [New escape sequence - `\e`](#new-escape-sequence).
1414
- [Method group natural type improvements](#method-group-natural-type)
1515
- [Implicit indexer access in object initializers](#implicit-index-access)
16+
- [Enable `ref` locals and `unsafe` contexts in iterators and async methods](#ref-and-unsafe-in-iterators-and-async-methods)
1617

1718
C# 13 is supported on **.NET 9**. For more information, see [C# language versioning](../language-reference/configure-language-version.md).
1819

@@ -73,6 +74,14 @@ var countdown = new TimerRemaining()
7374

7475
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.
7576

77+
# `ref` and `unsafe` in iterators and `async` methods
78+
79+
Prior to C# 13, iterator methods (methods that use `yield return`) and `async` methods couldn't declare local `ref` variables, nor could they have an `unsafe` context.
80+
81+
In C# 13, `async` methods can declare `ref` local variables, or local variables of a `ref struct` type. However, those variables can't be accessed across an `await` boundary. Neither can they be accessed across a `yield return` boundary.
82+
83+
This relaxed restriction enables the compiler to allow verifiably safe use of `ref` local variables and `ref struct` types in more places. You can safely use types like <xref:System.ReadOnlySpan%601?displayProperty=nameWithType> in these methods. The compiler tells you if you've violated safety rules.
84+
7685
## See also
7786

7887
- [What's new in .NET 9](../../core/whats-new/dotnet-9/overview.md)

0 commit comments

Comments
 (0)