async/await - when to return a Task vs void?

后端 未结 7 1181
梦如初夏
梦如初夏 2020-11-21 23:47

Under what scenarios would one want to use

public async Task AsyncMethod(int num)

instead of

public async void AsyncMetho         


        
7条回答
  •  旧时难觅i
    2020-11-22 00:11

    My answer is simple you can not await void method

    Error   CS4008  Cannot await 'void' TestAsync   e:\test\TestAsync\TestAsyncProgram.cs
    

    So if the method is async it is better to be awaitable, because you can loose async advantage.

提交回复
热议问题