-
Notifications
You must be signed in to change notification settings - Fork 6k
Improve "Async return types" docs #47289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @xakep139
Thanks for spotting this. However, the fix isn't correct either. I looked back at the spec and made a suggestion for different text.
@@ -71,7 +71,7 @@ The following example shows the behavior of an async event handler. In the examp | |||
|
|||
## Generalized async return types and ValueTask\<TResult\> | |||
|
|||
An async method can return any type that has an accessible `GetAwaiter` method that returns an instance of an *awaiter type*. In addition, the type returned from the `GetAwaiter` method must have the <xref:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute?displayProperty=nameWithType> attribute. You can learn more in the article on [Attributes read by the compiler](../language-reference/attributes/general.md#asyncmethodbuilder-attribute) or the C# spec for the [Task type builder pattern](~/_csharpstandard/standard/classes.md#15142-task-type-builder-pattern). | |||
An async method can return any type that has an accessible `GetAwaiter` method that returns an instance of an *awaiter type*. In addition, that type with a `GetAwaiter` method must have the <xref:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute?displayProperty=nameWithType> attribute. You can learn more in the article on [Attributes read by the compiler](../language-reference/attributes/general.md#asyncmethodbuilder-attribute) or the C# spec for the [Task type builder pattern](~/_csharpstandard/standard/classes.md#15142-task-type-builder-pattern). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither the original article nor this PR is correct. Let's try this instead:
An async method can return any type that has an accessible `GetAwaiter` method that returns an instance of an *awaiter type*. In addition, that type with a `GetAwaiter` method must have the <xref:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute?displayProperty=nameWithType> attribute. You can learn more in the article on [Attributes read by the compiler](../language-reference/attributes/general.md#asyncmethodbuilder-attribute) or the C# spec for the [Task type builder pattern](~/_csharpstandard/standard/classes.md#15142-task-type-builder-pattern). | |
An async method can return any type that has an accessible `GetAwaiter` method that returns an instance of an *awaiter type*. In addition, the returned type must match the type of the parameter of `SetResult` and returned type of the `Task` property on the type specified by the <xref:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute?displayProperty=nameWithType> attribute. You can learn more in the article on [Attributes read by the compiler](../language-reference/attributes/general.md#asyncmethodbuilder-attribute) or the C# spec for the [Task type builder pattern](~/_csharpstandard/standard/classes.md#15142-task-type-builder-pattern). |
Summary
This PR rephrases a misleading statement regarding an
awaiter type
- anAsyncMethodBuilderAttribute
should be applied on a type that an async method returns, not on a type thatGetAwaiter()
method returns.The same requirement is showcased in the spec - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#15141-general.
As an example:
Rigth now the documentation states that an
AsyncMethodBuilderAttribute
should be applied on aCustomAwaiter
type from the example above, which is misleading.Internal previews