C# Error CS4032 – The ‘await’ operator can only be used within an async method. Consider marking this method with the ‘async’ modifier and changing its return type to ‘Task<{0}>‘.

C# Error

CS4032 – The ‘await’ operator can only be used within an async method. Consider marking this method with the ‘async’ modifier and changing its return type to ‘Task‘.

Reason for the Error & Solution

The ‘await’ operator can only be used within an async method. Consider marking this method with the ‘async’ modifier and changing its return type to ‘Task<T>’.

Example

The following sample generates CS4032:

// CS4032.cs (7,9)
using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
    static IAsyncEnumerator<int> M(int value)
    {
        yield return value;
        await Task.CompletedTask;
    }
}

To correct this error

To correct this error, change the signature of method M to make it asynchronous:

using System.Collections.Generic;
using System.Threading.Tasks;

class C
{
    static async IAsyncEnumerator<int> M(int value)
    {
        yield return value;
        await Task.CompletedTask;
    }
}

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...