Replies: 1 comment 1 reply
-
This would be a good place where you could write an analyzer for this rule :-) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi .NET team,
I would like to propose an enhancement to the .NET compiler to introduce a warning when an async method does not return a Task (or Task), regardless of its current return type, including void.
Proposal:
Introduce a compiler warning for any method marked as async that does not return Task or Task.
The only exception to this rule would be event handlers (where async void is often necessary).
Justification:
One of the core principles of asynchronous programming in .NET is that async methods should return Task or Task to allow proper task chaining and exception handling.
Returning anything other than Task (including void, or even non-task types) from an async method breaks the asynchronous model by making it impossible to await the method or catch exceptions that might occur within it.
Common cases where async void or other return types are used (outside of event handlers) often lead to subtle bugs related to unhandled exceptions and hard-to-trace task completion states.
By introducing this warning, we can guide developers towards a more consistent and safer async programming model. This would help avoid potential mistakes such as:
Missed exceptions in non-awaited async void methods.
Difficulties in tracking the completion of asynchronous methods when Task is not used.
The only justified use of async void should be in event handlers, where the method signature is predefined and doesn't allow returning Task. For all other cases, returning Task should be the default behavior.
Thank you for considering this suggestion! I believe this change could significantly improve async programming practices in .NET by encouraging developers to follow the intended patterns for asynchronous methods.
Best regards,
Christophe Feltz
Beta Was this translation helpful? Give feedback.
All reactions