Can’t await async extension method

The error message is pretty clear: the method where you’re calling the extension method should be marked as async.

public Task<string> MyExtension(this string s) { ... }

public async Task MyCallingMethod()
{    
    string result = await "hi".MyExtension();
}

Re-reading this part should make much more sense now:

“The ‘await’ operator can only be used within an async method. “

Leave a Comment