在C#中“回程等待”的目的是什么?在那里嗎任何這樣編寫方法的場景:public async Task<SomeResult> DoSomethingAsync(){
// Some synchronous code might or might not be here... //
return await DoAnotherThingAsync();}而不是這樣:public Task<SomeResult> DoSomethingAsync(){
// Some synchronous code might or might not be here... //
return DoAnotherThingAsync();}會有意義嗎?為什么使用return await構(gòu)造可以直接返回的Task<T>從內(nèi)部DoAnotherThingAsync()召喚?我看到代碼return await在很多地方,我想我可能錯過了什么。但據(jù)我所知,在這種情況下不使用異步/等待關(guān)鍵字并直接返回任務(wù)在功能上是等價的。為什么要增加額外的開銷await層?
3 回答

波斯汪
TA貢獻1811條經(jīng)驗 獲得超4個贊
return
return await
async
using
return await
try
Task<SomeResult> DoSomethingAsync(){ using (var foo = new Foo()) { return foo.DoAnotherThingAsync(); }}async Task<SomeResult> DoSomethingAsync(){ using (var foo = new Foo()) { return await foo.DoAnotherThingAsync(); }}
Dispose()
Foo
DoAnotherThingAsync()
Foo

躍然一笑
TA貢獻1826條經(jīng)驗 獲得超6個贊
await
try/catch
async
.
- 3 回答
- 0 關(guān)注
- 531 瀏覽
添加回答
舉報
0/150
提交
取消