ValueTask<TResult> Struct


Provides a value type that wraps a Task{TResult} and a TResult, only one of which is used.

public struct ValueTask<TResult> : IEquatable<System.Threading.Tasks.ValueTask<TResult>>
Type Parameters

The result.



A method may return an instance of this value type when it's likely that the result of its operation will be available synchronously, and when it's expected to be invoked so frequently that the cost of allocating a new Task<TResult> for each call will be prohibitive.

There are tradeoffs to using a ValueTask<TResult> instead of a Task<TResult>. For example, while a ValueTask<TResult> can help avoid an allocation in the case where the successful result is available synchronously, it also contains two fields, whereas a Task<TResult> as a reference type is a single field. This means that a method call returns two fields worth of data instead of one, which is more data to copy. It also means, that if a method that returns a ValueTask<TResult> is awaited within an async method, the state machine for that async method will be larger, because it must store a struct containing two fields instead of a single reference.

For uses other than consuming the result of an asynchronous operation using await, ValueTask<TResult> can lead to a more convoluted programming model that requires more allocations. For example, consider a method that could return either a Task<TResult> with a cached task as a common result or a ValueTask<TResult>. If the consumer of the result wants to use it as a Task<TResult> in a method like WhenAll or WhenAny, the ValueTask<TResult> must first be converted to a Task<TResult> using AsTask, leading to an allocation that would have been avoided if a cached Task<TResult> had been used in the first place.

As such, the default choice for any asynchronous method should be to return a Task or Task<TResult>. Only if performance analysis proves it worthwhile should a ValueTask<TResult> be used instead of a Task<TResult>. There is no non-generic version of ValueTask<TResult>, as the CompletedTask property may be used to hand back a successfully completed singleton in the case where a method returning a Task completes synchronously and successfully.


The use of the type is supported starting with C# 7, and is not supported by any version of Visual Basic.



Initializes a new instance of the ValueTask{TResult} class using the supplied task that represents the operation.


Initializes a new instance of the ValueTask{TResult} class using the supplied result of a successful operation.



Gets a value that indicates whether this object represents a canceled operation.


Gets a value that indicates whether this object represents a completed operation.


Gets a value that indicates whether this object represents a successfully completed operation.


Gets a value that indicates whether this object represents a failed operation.


Gets the result.



Retrieves a Task{TResult} object that represents this ValueTask{TResult}.


Configures an awaiter for this value.


Creates a method builder for use with an async method.


Determines whether the specified object is equal to the current object.


Determines whether the specified ValueTask{TResult} object is equal to the current ValueTask{TResult} object.


Creates an awaiter for this value.


Returns the hash code for this instance.


Returns a string that represents the current object.


Equality(ValueTask<TResult>, ValueTask<TResult>)

Compares two values for equality.

Inequality(ValueTask<TResult>, ValueTask<TResult>)

Determines whether two ValueTask{TResult} values are unequal.

Applies to