Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Task vs ValueTask: Choosing the Right Async Return Type

Understanding the differences between Task and ValueTask in C#, when each return type is appropriate, and how ValueTask can reduce allocations in performance-sensitive code.

~5 min readC#
#C##async#ValueTask#Task
Illustration comparing Task and ValueTask in C#, showing a heap allocation versus a stack-based struct.

When you write an async method in C#, the return type is usually Task or Task<T>. But ValueTask and ValueTask<T> offer a lower-allocation alternative. The choice between Task and ValueTask in C# comes down to how often the method completes synchronously, how many times the result is awaited, and whether the extra complexity is justified. This article explains the practical differences and gives concrete guidance for selecting the right type.

What Task and ValueTask Represent

Task is a reference type that represents an asynchronous operation. It can be awaited multiple times, stored in fields, and used as a general-purpose handle for any async operation. ValueTask is a struct that wraps either a successful value or a reference to a Task (or an IValueTaskSource). It is designed for scenarios where the operation frequently completes synchronously and where avoiding a heap allocation matters.

The most important difference is that ValueTask is a value type. Returning a ValueTask from a method does not allocate on the heap when the method completes synchronously. Task, being a class, can still require a heap allocation when an async method returns synchronously, although the runtime may cache some completed tasks. In hot paths where async methods are called frequently and often complete without awaiting, this allocation can add measurable pressure on the garbage collector.

How ValueTask Avoids Allocation

When an async method returns Task, the compiler generates a state machine and usually creates a Task object to represent the eventual result. If the method completes synchronously, that Task may still be allocated, although the runtime can cache some completed tasks. With ValueTask, the method can return the result directly as a struct. For example:

csharp
public ValueTask<int> GetNumberAsync() { return new ValueTask<int>(42); // no allocation }

If the method actually needs to await an asynchronous operation, it can return a ValueTask that wraps the underlying Task or a custom IValueTaskSource. The compiler handles this transparently when you use async:

csharp
public async ValueTask<int> GetValueAsync() { await Task.Delay(1); return 42; }

In the synchronous case, the struct is returned directly. If the method has to suspend, the state machine still has to survive on the heap, so the allocation savings are most direct on the synchronous path. In high-performance libraries, a ValueTask can also be backed by an IValueTaskSource that is reused across calls, which can reduce allocations on asynchronous paths as well. The exact allocation behavior depends on the .NET version and on what the ValueTask wraps.

When ValueTask Is a Good Choice

Use ValueTask when the method is likely to complete synchronously most of the time, and when it is called from a hot path where allocations are a concern. Common examples include:

  • Methods that check a cache and return a value without hitting an I/O operation.
  • Methods that wrap a synchronous operation but are exposed as async for API consistency.
  • High-frequency calls in a library where the caller may not need to await the result.

For instance, a cache lookup that often hits:

csharp
public async ValueTask<Customer> GetCustomerAsync(int id) { if (_cache.TryGetValue(id, out var customer)) { return customer; // synchronous completion, no allocation } customer = await _repository.GetCustomerAsync(id); _cache[id] = customer; return customer; }

In this pattern, the synchronous path avoids a Task allocation entirely, which can reduce GC pressure in a busy service.

When Task Is Still the Right Choice

Task remains the default choice for most async methods. It is simpler, more flexible, and does not impose the restrictions that come with ValueTask. Use Task when:

  • The method is part of a public API that may be consumed by code you do not control.
  • The result may be awaited multiple times (e.g., Task.WhenAny or Task.WhenAll).
  • The result may be stored in a field or used across threads.
  • The method is not on a hot path where allocation is a measurable issue.

ValueTask can only be awaited once. If you need to inspect the result multiple times, you must first call AsTask() to convert it to a Task. This is a common source of bugs when developers assume ValueTask behaves like Task.

Constraints and Pitfalls of ValueTask

Because ValueTask is a struct, it has several limitations that do not apply to Task:

  • Single await: You cannot await the same ValueTask twice. The underlying IValueTaskSource may be reused after the first await, leading to undefined behavior.
  • Avoid blocking: ValueTask<T> exposes a Result property, but blocking on it is not a supported consumption pattern. Use await instead; ValueTask and ValueTask<T> do not expose a direct Wait() method.
  • Boxing: Calling AsTask() on a ValueTask backed by an IValueTaskSource can allocate a Task, and using ValueTask as a generic type argument (e.g., in Task<ValueTask<T>>) boxes it. Both operations can defeat the allocation benefit.
  • Not thread-safe: The underlying IValueTaskSource may not be safe to access from multiple threads concurrently. Task is thread-safe for multiple awaits.

These constraints mean that ValueTask is not a drop-in replacement for Task. It is a performance optimization that requires careful usage.

Performance Considerations and Measurement

The main performance benefit of ValueTask is the elimination of a heap allocation on the synchronous completion path. Allocation is not the only cost; it also affects GC pressure and cache locality. However, the actual impact depends on the frequency of calls and the proportion of synchronous completions. Without profiling, you cannot assume that switching to ValueTask will make your code faster. The overhead of the struct itself is small, but the added complexity and the risk of misuse may outweigh the benefit in many cases.

A reasonable approach is to profile the application and identify hot paths where async methods are called frequently and often complete synchronously. If allocation is a measurable bottleneck, consider ValueTask. Otherwise, stick with Task for simplicity.

Decision Guidance for API Design

When designing a public API, think about the consumers. If you expose a method that returns ValueTask, you require every caller to understand its limitations. A library that returns ValueTask from a method that is often awaited once can be a good fit, but you must document that the result cannot be cached or awaited multiple times.

A common pattern is to use ValueTask internally for performance-critical code and expose Task in the public API. This keeps the implementation efficient while preserving a familiar contract for callers.

For example, an internal helper might use ValueTask to avoid allocations, while the public method wraps it in a Task:

csharp
public async Task<Data> GetDataAsync() { return await GetDataCoreAsync(); } private async ValueTask<Data> GetDataCoreAsync() { // fast path often completes synchronously }

This approach limits the complexity to the implementation and keeps the public surface safe.

Compatibility and Versioning

ValueTask is part of the base class library in modern .NET (5 and later). For older target frameworks, verify availability; when it is not included, the System.Threading.Tasks.Extensions package provides it. Changing a public return type from Task to ValueTask is a breaking change: it changes the method signature, so existing caller source may need updates, and binaries compiled against the old signature should be treated as incompatible. For most applications, Task remains the safer default; ValueTask is a targeted optimization for specific hot paths.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.