强制执行此值并返回其结果。与 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 8,Windows 7,Windows server 2012中,Windows server 2008 R2
版本信息
F#核心库版本
受以下版本支持:2.0