Reusing the <T> task in .NET core / .NET Standard
Is it possible to reuse the same task <T> created using Task.FromResult for instant return methods (since it is already completed)?
My reasoning is to reduce the garbage created by implementations of asynchronous interfaces that exit instantly (Task <bool> will be a great example for it, since it has only two possible values).
The basics in this article about recycling tasks should be possible if .NET Core behaves the same (is it?).
Yes, and this is highly recommended in cases where you have a small set of probable results that can be known synchronously (from the cache, etc.). Similarly, in the case of Task (not Task<T> ), you can use Task.CompletedTask .
Note that if most of your calls end synchronously, but you donβt have a small domain with likely results, you can consider ValueTask<T> , which is optimized for this case.
Everything here applies equally to .NET and .NET Core.