Edit

Share via


Compiler Error CS8178

'await' cannot be used in an expression containing a call to because it returns by reference

Example

The following sample generates CS8178:

using System;
using System.Threading.Tasks;

class TestClass
{
    int x;
    ref int Save(int y)
    {
        x = y;
        return ref x;
    }

    async Task TestMethod()
    {
        Save(1) = await Task.FromResult(0);
    }
}

To correct this error

Changing the use of the return by reference to be synchronous corrects the error:

    async Task TestMethod()
    {
        var x = await Task.FromResult(0);
        Save(1) = x;
    }