共用方式為


Lazy.Force<'T> 擴充方法 (F#)

更新:2010 年 5 月

強制執行此值並傳回其結果。 與 Value 相同。 可使用互斥來防止其他執行緒同時計算此值。

命名空間/模組路徑:Microsoft.FSharp.Control.LazyExtensions

組件:FSharp.Core (在 FSharp.Core.dll 中)

// Signature:
type System.Lazy with
  member Force : unit -> 'T

// Usage:
lazy.Force ()

傳回值

Lazy 物件的值。

範例

下列程式碼會說明如何使用 Force延伸方法。

let lazyFactorial n = Lazy.Create (fun () ->
    let rec factorial n =
        match n with
        | 0 | 1 -> 1
        | n -> n * factorial (n - 1)
    factorial n)
let printLazy (lazyVal:Lazy<int>) =
    if (lazyVal.IsValueCreated) then
        printfn "Retrieving stored value: %d" (lazyVal.Value)
    else
        printfn "Computing value: %d" (lazyVal.Force())
let lazyVal1 = lazyFactorial 12
let lazyVal2 = lazyFactorial 10
lazyVal1.Force() |> ignore
printLazy lazyVal1
printLazy lazyVal2

輸出指示時 Force呼叫以建立的值 lazyVal1、 計算的值擷取時列印值。

  

平台

Windows 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

版本資訊

F# 執行階段

支援版本:2.0

Silverlight

支援版本:3

請參閱

參考

Control.LazyExtensions 模組 (F#)

Lazy<T>

延遲運算 (F#)

變更記錄

日期

History

原因

2010 年 5 月

加入程式碼範例。

資訊加強。