What is the best way to return completed Task?

后端 未结 3 1652
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 05:52

What is the best way to return a completed Task object?

It is possible to write Task.Delay(0), or Task.FromResult(true) whatever.

But what is the

3条回答
  •  感动是毒
    2021-01-29 06:49

    Task.FromResult would be the most direct. It also includes inbuilt results for a few common integers etc. However, if your value is not an "obvious" value (and won't have inbuilt handling) but is likely to be returned often in your scenario - then you can create your own cached result in a field (maybe static if appropriate) - but it is important to cache the Task, not the result itself.l - otherwise just use Task.FromResult each time.

提交回复
热议问题