Sometimes I create a method like this
Task<int> f()
{
if (...) return Task.Factory.StartNew(() => 42); // in this case, result already known
else ... // return some "real task"
}
But I was wondering if there is a way to create a task thas is already completed, so that I won’t incur any potential overhead of scheduling the “calculation” 42
Use
TaskCompletionSource<T>:(via MSDN)