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> AsTask無法接受這些限制, 請呼叫將轉換成。 Task<TResult>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>想要在或WhenAny之類WhenAll的方法中使用它做為Task<TResult> , 則ValueTask<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.

因此, 任何非同步方法的預設選擇應該是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.

適用於