Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
'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;
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.