C# Error CS1983 – The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator

C# Error

CS1983 – The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator

Reason for the Error & Solution

The return type of an async method must be void, Task, Task<T>, a task-like type, IAsyncEnumerable<T>, or IAsyncEnumerator<T>

Example

The following sample generates CS1983:

// CS1983.cs (4,62)
using System.Collections.Generic;

class C
{
    static async IEnumerable<int> M()
    {
        yield return await Task.FromResult(1);
    }
}

Since IEnumerable.GetEnumerator returns an IEnumerator<T> whose MoveNext method does not return a value of Task<T>, it is not compatible with an async method.

To correct this error

Use an interface that results in the invocation of method that returns a type of Task<T>, e.g., IAsyncEnumerable<T>:

using System.Collections.Generic;

class C
{
    static async IAsyncEnumerable<int> M()
    {
        yield return await Task.FromResult(1);
    }
}