ValueTask - Reduces Memory Allocations

ValueTask<T> avoids unnecessary heap allocations compared to Task<T>.


Before (More Allocations)

Returning Task<T> always causes heap allocation, even when returning a cached result.

public async Task<int> GetValueAsync()
{
    return await Task.FromResult(42); // Creates unnecessary Task object
}


After (Reduced Allocations with ValueTask<T>)

Using ValueTask<T>, allocations are avoided for completed tasks.

public ValueTask<int> GetValueAsync()
{
    return new ValueTask<int>(42); // No heap allocation
}