Export (0) Print
Expand All

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)

public:
Lazy(
	bool isThreadSafe
)

Parameters

isThreadSafe
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.

NoteNote

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.

NoteNote

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

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.

Show:
© 2014 Microsoft