Task is a class, so each async call that actually has to wait allocates an object. ValueTask is a struct that can wrap either a ready result or a Task, so a method that often finishes synchronously (for example, a cache hit) can return without any allocation.
Use ValueTask when:
the method is called very often, and
it frequently completes synchronously.
For everything else, Task is simpler and safer. ValueTask has rules: you should await it only once, you should not await it from several places at the same time, and you should not call .Result before it finishes. Breaking these gives bugs that are hard to find.
If you are writing normal application code such as controllers and services, stay with Task. Measure first before switching a hot path to ValueTask.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Task is a class, so each async call that actually has to wait allocates an object. ValueTask is a struct that can wrap either a ready result or a Task, so a method that often finishes synchronously (for example, a cache hit) can return without any allocation.
Use ValueTask when:
For everything else, Task is simpler and safer. ValueTask has rules: you should await it only once, you should not await it from several places at the same time, and you should not call .Result before it finishes. Breaking these gives bugs that are hard to find.
If you are writing normal application code such as controllers and services, stay with Task. Measure first before switching a hot path to ValueTask.