Can't await async extension method

后端 未结 1 1113
Happy的楠姐
Happy的楠姐 2020-12-01 22:24

Situation is pretty simple - I wrote an extension method and made it async with return type Task. But when I try to call it using await, compiler throw

相关标签:
1条回答
  • 2020-12-01 23:13

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

    0 讨论(0)
提交回复
热议问题