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) { /* ... */ }
}

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Gemini.Build.CodeGeneration
{
    public class Logger : ILogger
    {
        public static ILogger Log { get; set; } = NullLogger.Instance;

        void ILogger.Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
        {
            Log.Log(logLevel, eventId, state, exception, formatter);
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return Log.IsEnabled(logLevel);
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return Log.BeginScope(state);
        }
    }
}

Compliant Code Examples

class Compliant {
    async Task AsyncFetch() { /* ... */ }
    async void OnClick() { /* ... */ }
    async void HandleClick(object sender, EventArgs e) { /* ... */ }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: rtrieu/product-analytics-ui-changes