help
Root Question Message
ValueTask<T>
instead of Task<T>
, but when should you exactly do this? The extent of my knowledge (and reading the docs) is that ValueTask<T>
is like a DU between either a T
value or a Task<T>
, so is it ideal for situations in which an async method may just return a cached value?ValueTask<T>
as the result of an async method, is the compiler smart enough to generate the appropriate code based on whether such a cached value is used without any async calls being used?Sign In and Join Server To See
public async Task<string> GetStringAsync() {
if (cachedValue is not null) return cachedValue;
string value = await DoSomeOtherOperationAsync();
cachedValue = value;
return value;
}
Is it worth using ValueTask
?Sign In and Join Server To See
Sign In and Join Server To See
Sign In and Join Server To See
public Task<string> GetStringAsync() {
if (cachedValue is not null) return cachedValue;
string value = DoSomeOtherOperationAsync();
cachedValue = value;
return value;
}
Sign In and Join Server To See
return Task.FromResult(cachedValue);
Sign In and Join Server To See
Sign In and Join Server To See
Task<string>
(otherwise none of the other lines would compile)Sign In and Join Server To See
Sign In and Join Server To See
Sign In and Join Server To See
async
is ellided for perf. I assume MS knows how to do that properly...tagging the method names with Async
can be confusing.