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

備註

AValueTask<TResult>執行個體可能會等候或轉換成Task<TResult>使用AsTaskA ValueTask<TResult> instance may either be awaited or converted to a Task<TResult> using AsTask. AValueTask<TResult>執行個體可能只有一次,等候,而且取用者可能不會呼叫GetAwaiter()之前的執行個體已完成。A ValueTask<TResult> instance may only be awaited once, and consumers may not call GetAwaiter() until the instance has completed. 如果無法接受這些限制,將轉換ValueTask<TResult>Task<TResult>藉由呼叫AsTaskIf 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 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. 這也表示,如果方法會傳回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 two 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,則ValueTask<TResult>必須先轉換成Task<TResult>使用AsTask,而導致會有已盡可能配置快取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.

因此,任何非同步方法的預設選擇應該傳回TaskTask<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. CompletedTask屬性應該用來在的情況下成功完成的單一方法,傳回其中交給Task同步且已順利完成。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.

注意

善用ValueTask<TResult>開始便支援型別C#7.0 中,並不支援任何版本的 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)

屬性

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()
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.

適用於