开发者

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

开发者 https://www.devze.com 2023-04-08 05:46 出处:网络
On the MSDN documentation for Lazy.Force<T> extension method says: Forces the execution of this value and returns its result. Same as

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 thr开发者_StackOverfloweads 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


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


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号