Skip to content

Commit 2832342

Browse files
committed
Add attribute section for async iterator cancellation
In addition, add usings to the relevant template
1 parent cdf7e86 commit 2832342

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

standard/attributes.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,30 @@ Specifies that a nullable argument won’t be `null` when the method returns the
10891089
>
10901090
> *end example*
10911091
1092+
### §enumerator-cancellation The EnumeratorCancellation attribute
1093+
1094+
Specifies the parameter representing the `CancellationToken` for an asynchronous iterator15.15).
1095+
1096+
> *Example*: The method `GetStringsAsync()` is an asynchronous iterator. Before doing any work to retrieve the next value, it checks the cancellation token to determine if the iteration should be cancelled. If Cancellation is requested, no further action is taken.
1097+
>
1098+
> <!-- Example: {template:"code-in-class-lib", name:"AsyncEnumeratorCancellation"} -->
1099+
> ```csharp
1100+
> static async IAsyncEnumerable<object> GetStringsAsync([EnumeratorCancellation]CancellationToken token)
1101+
> {
1102+
> for (int i = 0; i < 10; i++)
1103+
> {
1104+
> if (token.IsCancellationRequested)
1105+
> {
1106+
> yield break; // Exit if cancellation is requested
1107+
> }
1108+
> await Task.Delay(100);
1109+
> yield return i.ToString();
1110+
> }
1111+
> }
1112+
> ```
1113+
>
1114+
> *end example*
1115+
10921116
## 22.6 Attributes for interoperation
10931117
10941118
For interoperation with other languages, an indexer may be implemented using indexed properties. If no `IndexerName` attribute is present for an indexer, then the name `Item` is used by default. The `IndexerName` attribute enables a developer to override this default and specify a different name.

tools/example-templates/code-in-class-lib/Library.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Security.Permissions;
1111
using System.Text;
1212
using System.Threading;
13+
using System.Threading.Tasks;
1314
using System.Diagnostics.CodeAnalysis;
1415

1516
partial class Class1

0 commit comments

Comments
 (0)