ValueTask<TResult> ValueTask<TResult> ValueTask<TResult> ValueTask<TResult> Struct

定义

提供包装 Task<TResult>TResult(仅使用其中之一)的值类型。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 struct ValueTask<TResult> : IEquatable<System.Threading.Tasks.ValueTask<TResult>>
type ValueTask<'Result> = struct
Public Structure ValueTask(Of TResult)
Implements IEquatable(Of ValueTask(Of TResult))

类型参数

TResult

结果。The result.

继承
ValueTask<TResult>ValueTask<TResult>ValueTask<TResult>ValueTask<TResult>
实现
IEquatable<ValueTask<TResult>>

注解

实例可以等待或Task<TResult>使用AsTask转换为。 ValueTask<TResult>A ValueTask<TResult> instance may either be awaited or converted to a Task<TResult> using AsTask. 实例仅可等待一次, 并且在实例完成之前, 使用者可能不会调用GetAwaiter()ValueTask<TResult>A ValueTask<TResult> instance may only be awaited once, and consumers may not call GetAwaiter() until the instance has completed. 如果这些限制不可接受, 请ValueTask<TResult> Task<TResult>通过调用AsTask将转换为。If these limitations are unacceptable, convert the ValueTask<TResult> to a Task<TResult> by calling AsTask.

永远不应对ValueTask<TResult>实例执行以下操作:The following operations should never be performed on a ValueTask<TResult> instance:

  • 等待实例多次。Awaiting the instance multiple times.
  • 多次AsTask调用。Calling AsTask multiple times.
  • .Result操作.GetAwaiter().GetResult()尚未完成或多次使用时使用或。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.

如果某个方法可能会以同步方式使用其操作的结果, 并且应经常调用该方法, 从而导致为每个调用分配一个新Task<TResult>的成本为不受支持, 则该方法可能返回此值类型的实例。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.

使用ValueTask<TResult>而不Task<TResult>是。There are tradeoffs to using a ValueTask<TResult> instead of a Task<TResult>. 例如, 虽然ValueTask<TResult>可以在成功结果以同步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. 这意味着ValueTask<TResult>从方法返回会导致复制更多数据。This means that returning a ValueTask<TResult> from a method results in copying more data. 这也意味着, 如果返回的ValueTask<TResult>方法在异步方法中等待, 则该异步方法的状态机将更大, 因为它必须存储包含多个字段而不是单个引用的结构。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.

除了使用 await 来使用异步操作的结果以外, ValueTask<TResult>它可能会导致更复杂的编程模型需要更多分配。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. 例如, 请考虑一个方法, 该方法可以Task<TResult>将替换为缓存任务作为常见结果ValueTask<TResult>或。For example, consider a method that could return either a Task<TResult> with a cached task as a common result or a ValueTask<TResult>. 如果结果的使用者Task<TResult>想要在类似于WhenAllWhenAny的方法中将其用作Task<TResult> , AsTaskValueTask<TResult>必须首先将转换为, 并在首先Task<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.

因此, 任何异步方法的默认选择都应为返回Task或。 Task<TResult>As such, the default choice for any asynchronous method should be to return a Task or Task<TResult>. 仅当性能分析证明需要使用时, ValueTask<TResult>才应使用, 而Task<TResult>不是。Only if performance analysis proves it worthwhile should a ValueTask<TResult> be used instead of a Task<TResult>. 大多数情况下, 不ValueTask建议使用的非泛型版本。The non generic version of ValueTask is not recommended for most scenarios. 此属性应该用于在方法Task返回成功完成的情况下, 手动返回已成功完成的单一实例。 CompletedTaskThe 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.

备注

支持从C# 7.0 开始ValueTask<TResult>使用该类型, 并且 Visual Basic 的任何版本都不支持此类型。The use of the ValueTask<TResult> type is supported starting with C# 7.0, and is not supported by any version of Visual Basic.

构造函数

ValueTask<TResult>(Task<TResult>) ValueTask<TResult>(Task<TResult>) ValueTask<TResult>(Task<TResult>) ValueTask<TResult>(Task<TResult>)

使用提供的表示操作的任务初始化 ValueTask<TResult> 类的新实例。Initializes a new instance of the ValueTask<TResult> class using the supplied task that represents the operation.

ValueTask<TResult>(TResult) ValueTask<TResult>(TResult) ValueTask<TResult>(TResult) ValueTask<TResult>(TResult)

使用提供的成功操作的结果初始化 ValueTask<TResult> 类的新实例。Initializes a new instance of the ValueTask<TResult> class using the supplied result of a successful operation.

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

使用表示操作的 IValueTaskSource<TResult> 对象初始化 ValueTask<TResult> 类的新实例。Initializes a new instance of the ValueTask<TResult> class with a IValueTaskSource<TResult> object that represents the operation.

属性

IsCanceled IsCanceled IsCanceled IsCanceled

获取指示此对象是否表示取消的操作的值。Gets a value that indicates whether this object represents a canceled operation.

IsCompleted IsCompleted IsCompleted IsCompleted

获取指示此对象是否表示完成的操作的值。Gets a value that indicates whether this object represents a completed operation.

IsCompletedSuccessfully IsCompletedSuccessfully IsCompletedSuccessfully IsCompletedSuccessfully

获取指示此对象是否表示成功完成的操作的值。Gets a value that indicates whether this object represents a successfully completed operation.

IsFaulted IsFaulted IsFaulted IsFaulted

获取指示此对象是否表示失败的操作的值。Gets a value that indicates whether this object represents a failed operation.

Result Result Result Result

获取结果。Gets the result.

方法

AsTask() AsTask() AsTask() AsTask()

检索表示此 ValueTask<TResult>Task<TResult> 对象。Retrieves a Task<TResult> object that represents this ValueTask<TResult>.

ConfigureAwait(Boolean) ConfigureAwait(Boolean) ConfigureAwait(Boolean) ConfigureAwait(Boolean)

为此值配置一个 awaiter。Configures an awaiter for this value.

CreateAsyncMethodBuilder() CreateAsyncMethodBuilder() CreateAsyncMethodBuilder() CreateAsyncMethodBuilder()

创建方法生成器以使用异步方法。Creates a method builder for use with an async method.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

Equals(ValueTask<TResult>) Equals(ValueTask<TResult>) Equals(ValueTask<TResult>) Equals(ValueTask<TResult>)

确定指定的 ValueTask<TResult> 对象是否等同于当前的 ValueTask<TResult>Determines whether the specified ValueTask<TResult> object is equal to the current ValueTask<TResult> object.

GetAwaiter() GetAwaiter() GetAwaiter() GetAwaiter()

为此值创建一个 awaiter。Creates an awaiter for this value.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

Preserve() Preserve() Preserve() Preserve()

获取将来随时可用的 ValueTask<TResult>Gets a ValueTask<TResult> that may be used at any point in the future.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

操作员

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

比较两个值是否相等。Compares two values for equality.

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

确定两个 ValueTask<TResult> 值是否不相等。Determines whether two ValueTask<TResult> values are unequal.

适用于