What is the best way to return a completed Task
object?
It is possible to write Task.Delay(0)
, or Task.FromResult<bool>(true)
whatever.
But what is the most efficient way?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With