Редагувати

Поділитися через


ValueTask<TResult> Struct

Definition

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

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

Type Parameters

TResult

The result.

Inheritance
ValueTask<TResult>
Implements
IEquatable<ValueTask<TResult>>

Remarks

A ValueTask<TResult> instance may either be awaited or converted to a Task<TResult> using AsTask. A ValueTask<TResult> 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<TResult> to a Task<TResult> by calling AsTask.

The following operations should never be performed on a ValueTask<TResult> 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<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 multiple fields, whereas a Task<TResult> as a reference type is a single field. This means that returning a ValueTask<TResult> from a method results in copying more data. 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 multiple 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>. 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<TResult> 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<TResult>) syntax (a zero-initialized structure) represents a synchronously, successfully completed operation with a result of default(TResult).

Constructors

ValueTask<TResult>(IValueTaskSource<TResult>, Int16)

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

ValueTask<TResult>(Task<TResult>)

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

ValueTask<TResult>(TResult)

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

Properties

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

AsTask()

Retrieves a Task<TResult> object that represents this ValueTask<TResult>.

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<TResult>)

Determines whether the specified ValueTask<TResult> object is equal to the current ValueTask<TResult> object.

GetAwaiter()

Creates an awaiter for this value.

GetHashCode()

Returns the hash code for this instance.

Preserve()

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

ToString()

Returns a string that represents the current object.

Operators

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