假设我有一个名为 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;
}
现在我有一个等待方法,它只做一件事 call fooAsync()
.
public Task<string> BarAsync(string foo, string bar)
{
return this.FooAsync(foo, bar);
}
我的问题是:在 BarAsync 中,我应该使用 return await this.FooAsync()
或return this.FooAsync()
为什么?
我尝试环顾四周,但尚未找到此模式的决定性答案。
最佳答案
My question here is that: inside BarAsync, should I use return await this.FooAsync() or return this.FooAsync() and why?
您应该使用return FooAsync(foo, bar)
。在这种情况下,除了调用另一个异步方法之外,您没有执行任何操作,因此请避免 async
和await
,您就避免了少量的开销。
关于c# - 在方法内返回await fooAsync()或return fooAsync(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677086/