C# & CLR Internals (50 Qs)•Part 3: CLR Internals, Native AOT & Production Diagnostics (25 - 35)
Async/Await: `Task` vs `ValueTask<T>`.
senior LevelMost AskedAsync & Promises
In Simple Words:
Task<T>: A reference type allocated on the heap every time an async method is called.ValueTask<T>: A value type struct. If the result is already available synchronously (e.g. from an in-memory cache or dictionary lookup),ValueTask<T>returns the value with zero heap allocation! If asynchronous work is required, it wraps a regularTask.- Rule: Use
ValueTask<T>for high-throughput methods that frequently return synchronously from cache.