Lazy<'T> Constructor (Boolean)


Initializes a new instance of the Lazy<'T> class. When lazy initialization occurs, the default constructor of the target type and the specified initialization mode are used.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

new : 
        isThreadSafe:bool -> Lazy


Type: System.Boolean

true to make this instance usable concurrently by multiple threads; false to make the instance usable by only one thread at a time.

The thread safety mode of a Lazy<'T> instance that is initialized with this constructor is LazyThreadSafetyMode.ExecutionAndPublication if isThreadSafe is true; otherwise, the mode is LazyThreadSafetyMode.None. The thread safety mode describes the behavior when multiple threads try to initialize the Lazy<'T> instance. To specify the LazyThreadSafetyMode.PublicationOnly mode, use the Lazy<'T>(Func<'T>, LazyThreadSafetyMode) or Lazy<'T>(LazyThreadSafetyMode) constructor.

A Lazy<'T> instance that is created with this constructor does not cache exceptions. For more information, see theLazy<'T> class or the System.Threading.LazyThreadSafetyMode enumeration.

The following example demonstrates the use of this constructor to create a lazy initializer that is not thread safe, for scenarios where all access to the lazily initialized object occurs on the same thread. It also demonstrates the use of the Lazy<'T>(LazyThreadSafetyMode) constructor (specifying LazyThreadSafetyMode.None for mode. To switch to a different constructor, just change which constructor is commented out.


For code that demonstrates how to use this constructor in multithreaded scenarios (specifying true for isThreadSafe), see the example for the Lazy<'T>() constructor.

The example defines a LargeObject class that will be initialized lazily. In the Main method, the example creates a Lazy<'T> instance and then pauses. When you press the Enter key, the example accesses the Value property of the Lazy<'T> instance, which causes initialization to occur. The constructor of the LargeObject class displays a console message.


For simplicity, this example uses a global instance of Lazy<'T>, and all the methods are static (Shared in Visual Basic). These are not requirements for the use of lazy initialization.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top