Language

ValueTask Struct

Definition

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

generic 
public value class ValueTask : IEquatable>
public readonly struct ValueTask : IEquatable>
public struct ValueTask : IEquatable>
type ValueTask<'Result> = struct
Public Structure ValueTask(Of TResult)
Implements IEquatable(Of ValueTask(Of TResult))

Type Parameters

TResult

The result.

Inheritance
ValueTask
Implements

Remarks

A ValueTask instance may either be awaited or converted to a Task using AsTask. A ValueTask instance may only be awaited once, and consumers may not read Result until the instance has completed. If these limitations are unacceptable, convert the ValueTask to a Task by calling AsTask.

The following operations should never be performed on a ValueTask instance:

  • Awaiting the instance multiple times.
  • Calling AsTask multiple times.
  • Using .Result or .GetAwaiter().GetResult() when the operation hasn't yet completed, or using them multiple times.
  • Using more than one of these techniques to consume the instance.

If you do any of the above, the results are undefined.

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 for each call will be prohibitive.

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

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

As such, the default choice for any asynchronous method should be to return a Task or Task. Only if performance analysis proves it worthwhile should a ValueTask be used instead of a Task. The non generic version of ValueTask is not recommended for most scenarios. The CompletedTask property should be used to hand back a successfully completed singleton in the case where a method returning a Task completes synchronously and successfully.

Note

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

Note

An instance created with the parameterless constructor or by the default(ValueTask) syntax (a zero-initialized structure) represents a synchronously, successfully completed operation with a result of default(TResult).

Constructors

Name Description
ValueTask(IValueTaskSource, Int16)

Initializes a new instance of the ValueTask class with a IValueTaskSource object that represents the operation.

ValueTask(Task)

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

ValueTask(TResult)

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

Properties

Name Description
IsCanceled

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

IsCompleted

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

IsCompletedSuccessfully

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

IsFaulted

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

Result

Gets the result.

Methods

Name Description
AsTask()

Retrieves a Task object that represents this ValueTask.

ConfigureAwait(Boolean)

Configures an awaiter for this value.

CreateAsyncMethodBuilder()

Creates a method builder for use with an async method.

Equals(Object)

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

Equals(ValueTask)

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

GetAwaiter()

Creates an awaiter for this value.

GetHashCode()

Returns the hash code for this instance.

Preserve()

Gets a ValueTask that may be used at any point in the future.

ToString()

Returns a string that represents the current object.

Operators

Name Description
Equality(ValueTask, ValueTask)

Compares two values for equality.

Inequality(ValueTask, ValueTask)

Determines whether two ValueTask values are unequal.

Applies to