What is the best way to return completed Task?

后端 未结 3 1567
被撕碎了的回忆
被撕碎了的回忆 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:56

    Answer from Stephen Toub (MSFT):

    If you want a new Task object each time, Task.FromResult is the most efficient. Task.Delay(0) in its current implementation will return a cached task, but that's an implementation detail. If you want to use a cached task, you should cache one yourself, e.g. private static readonly Task s_completedTask = Task.FromResult(true); and then use s_completedTask.

提交回复
热议问题