Pregunta

I am wondering what is the difference between two constructor overloads of Lazy<T> class.

Lazy(Func, Boolean) - Initializes a new instance of the Lazy class. When lazy initialization occurs, the specified initialization function and initialization mode are used.

Lazy(Func, LazyThreadSafetyMode) - Initializes a new instance of the Lazy class that uses the specified initialization function and thread-safety mode.

Is the second constructor more flexible from the point of thread-safety? Which LazyThreadSafetyMode member is an analogue for Lazy<...>(..., true)?

¿Fue útil?

Solución

From Reflector:

public Lazy(Func<T> valueFactory, bool isThreadSafe) : this(valueFactory, isThreadSafe ?LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
{
}

So if you pass true it will convert to LazyThreadSafetyMode.ExecutionAndPublication.

false will convert to LazyThreadSafetyMode.None

By the way, if you only pass the value factor (omitting the bool altogether), it uses LazyThreadSafetyMode.ExecutionAndPublication.

Otros consejos

If you pass true the object is initialized with the ExecutionAndPublication lazy mode. If you pass false the object is initialized with the None mode.

If you use an overload that does not take a boolean it is initialized with the ExecutionAndPublication mode.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top