Task.FromResult(whatever)
works for Task<TResult>
, but until 4.6 there was nothing for the nongeneric task. You could use FromResult
with a dummy value and implicitly cast it to Task
, but that somewhat obfuscates the intent (you're not really returning any asynchronous value) and allocates objects underneath (whereas CompletedTask
can be cached and shared between all callers).
It's not uncommon to see custom static completed tasks in current (4.5.2 and older) codebases, so in my opinion it makes sense to incorporate it to the framework itself.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…