What's the difference between Lazy.Force() and Lazy.Value

不想你离开。 提交于 2020-06-27 07:22:18

问题


On the MSDN documentation for Lazy.Force<T> extension method says:

Forces the execution of this value and returns its result. Same as Value. Mutual exclusion is used to prevent other threads from also computing the value.

Does it mean that it's equivalent to creating a Lazy<T> instance with ExecutionAndPublication LazyThreadSafetyMode so that only one thread can initialize the instance?

Thanks


回答1:


Yes. They are both the same, and both make sure that the value will be computed only once.




回答2:


It's a bit strange that the method exists and is not a function - it's not composable and replicates the behavior of .Value.

I would define something like the following (as seen in the F# compiler):

module Lazy =
    // Lazy.force : Lazy<'T> -> 'T
    let force (x: Lazy<'T>) = x.Force()


来源:https://stackoverflow.com/questions/7569310/whats-the-difference-between-lazy-force-and-lazy-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!