HomeCSharpC# 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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...