Detects improper usage of void return in an async method

Metadata

ID: csharp-best-practices/async-task-not-void

Language: C#

Severity: Error

Category: Best Practices

Description

According to the task asynchronous programming (TAP) model, async methods should only return void if they are event handlers. Otherwise, they should return Task or Task<TResult>

Non-Compliant Code Examples

class NonCompliant {
    async void AsyncFetch() { /* ... */ }
    async void Click() { /* ... */ }
    async void HandleClick(object sender, EventArgs e, string notEventHandlerDelegateSignature) { /* ... */ }
}

Compliant Code Examples

class Compliant {
    async Task AsyncFetch() { /* ... */ }
    async void OnClick() { /* ... */ }
    async void HandleClick(object sender, EventArgs e) { /* ... */ }
}