July 2017
Beginner
466 pages
9h 37m
English
Prior to C# 7.0, async methods had to return either void, Task, or Task<T>. As Task is a reference type, returning such an object from async methods can impact performance because it allocates an object into memory even though it returns a cached object or runs asynchronously.
To overcome this, C# 7.0 introduces the ValueTask type, which is set to prevent the allocation of a Task<T> object when the result of the async operation is already available. Using it, the async methods can return types other than Task, Task<T>, and void:
public async ValueTask<long> GetValue()
{
return await Task.Run<long>(() => 5000);
}
If you receive an error accessing ValueTask in C# 7.0, you must explicitly reference ...
Read now
Unlock full access