Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 514
theburningmonk Avatar asked Sep 27 '11 12:09

theburningmonk


2 Answers

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()
like image 143
AndreasHassing Avatar answered Nov 16 '22 00:11

AndreasHassing


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

like image 35
Ramon Snir Avatar answered Nov 16 '22 01:11

Ramon Snir