更新:2010 年 5 月
创建一个在强制执行时的计算结果为给定值的延迟计算。
命名空间/模块路径: Microsoft.FSharp.Control.LazyExtensions
程序集:FSharp.Core(在 FSharp.Core.dll 中)
// Signature:
type System.Lazy with
  member static CreateFromValue : Lazy<'T>
// Usage:
lazy.CreateFromValue (value)
参数
- value 
 类型:'T- 输入值。 
返回值
创建的 Lazy 对象。
示例
下面的代码示例演示 Lazy.CreateFromValue 扩展方法的用法。 在此示例中,字典用于存储之前计算的值。 调用阶乘函数时,如果已计算出值,则用缓存的结果调用 Lazy.CreateFromValue。 如果未计算值,则将使用 Lazy.Create。
let cacheMap = new System.Collections.Generic.Dictionary<_, _>()
cacheMap.Add(0, 1I)
cacheMap.Add(1, 1I)
let lazyFactorial n =
    let rec factorial n =
        if cacheMap.ContainsKey(n) then cacheMap.[n] else
        let result = new System.Numerics.BigInteger(n) * factorial (n - 1)
        cacheMap.Add(n, result)
        result
    if cacheMap.ContainsKey(n) then
        printfn "Reading factorial for %d from cache." n
        Lazy.CreateFromValue(cacheMap.[n])
    else
        printfn "Creating lazy factorial for %d." n
        Lazy.Create (fun () ->
            printfn "Evaluating lazy factorial for %d." n
            let result = factorial n
            result)
printfn "%A" ((lazyFactorial 12).Force())
printfn "%A" ((lazyFactorial 10).Force())
printfn "%A" ((lazyFactorial 11).Force())
printfn "%A" ((lazyFactorial 30).Force())
Output
平台
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#)
修订记录
| Date | 修订记录 | 原因 | 
|---|---|---|
| 2010 年 5 月 | 添加了代码示例。 | 信息补充。 |