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.
Assembly: mscorlib (in mscorlib.dll)
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.
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.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.