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);
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...