【发布时间】:2014-03-27 02:22:01
【问题描述】:
假设我有一个名为fooAsync()的方法:
public Task<T> FooAsync<T>(T foo, T bar)
{
// do some stuff
var returnFoo = await netLibraryMethodAsync(x, y, z);
// do some more stuff
return returnFoo as T;
}
现在我有一个 awaiter 方法,它只做一件事 call fooAsync()。
public Task<string> BarAsync(string foo, string bar)
{
return this.FooAsync(foo, bar);
}
我的问题是:在 BarAsync 中,我应该使用return await this.FooAsync() 还是return this.FooAsync(),为什么?
我已尝试环顾四周,但没有找到此模式的最终答案。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
@JohnSaunders:明白,先生。
-
我认为你应该返回 this.FooAsync(foo, bar);并由调用者决定是否等待。
-
它们基本相同(除非在调用 FooAsync 之前您的 BarAsync 中有其他等待)。
标签: c# asynchronous async-await