相关疑难解决方法(0)

在C#中"返回等待"的目的是什么?

没有像这样编写方法的场景:

public async Task<SomeResult> DoSomethingAsync()
{
    // Some synchronous code might or might not be here... //
    return await DoAnotherThingAsync();
}
Run Code Online (Sandbox Code Playgroud)

而不是这个:

public Task<SomeResult> DoSomethingAsync()
{
    // Some synchronous code might or might not be here... //
    return DoAnotherThingAsync();
}
Run Code Online (Sandbox Code Playgroud)

会有意义吗?

为什么return await在可以直接Task<T>从内部DoAnotherThingAsync()调用返回时使用构造?

return await在很多地方看到代码,我想我应该错过一些东西.但据我了解,在这种情况下不使用async/await关键字并直接返回Task将在功能上等效.为什么要增加附加await层的额外开销?

.net c# async-await .net-4.5

219
推荐指数
7
解决办法
3万
查看次数

标签 统计

.net ×1

.net-4.5 ×1

async-await ×1

c# ×1