Take a look at this code:
public async Task<int> GetResult(string param1)
{
if(param1 == "abc")
return _knownResult;
else
return await LongDatabaseCall();
}
Given that LogDatabaseCall
will return a Task<int>
, which might be saved into _knownResult
, what should be the type of _knownResult
— Task<int>
or ValueTask<int>
, performance-wise?
That's almost never the main issue, actually.
When determining whether you should use
Task<T>
orValueTask<T>
, the main issue is one of developer training: if everyone on your team is familiar with the pitfalls ofValueTask<T>
, then I'd recommend that. However, if not all the coders are familiar withValueTask<T>
(which seems to be true in the general case right now), then I'd recommendTask<T>
.The performance aspects are almost always immaterial.