Export (0) Print
Expand All

Lazy<T> Constructor (Func<T>, Boolean)

Initializes a new instance of the Lazy<T> class. When lazy initialization occurs, the specified initialization function and initialization mode are used.

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

public:
Lazy(
	Func<T>^ valueFactory, 
	bool isThreadSafe
)

Parameters

valueFactory
Type: System::Func<T>

The delegate that is invoked to produce the lazily initialized value when it is needed.

isThreadSafe
Type: System::Boolean

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

ExceptionCondition
ArgumentNullException

valueFactory is nullptr.

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.

Exceptions that are thrown by valueFactory are cached. For more information, see the Lazy<T> class or the System.Threading::LazyThreadSafetyMode enumeration.

The following example demonstrates the use of this constructor to provide lazy initialization with exception caching, in a scenario with a single thread. It also demonstrates the use of the [M:System.Lazy`1.#ctor(System.Func`1,System.Threading.LazyThreadSafetyMode)] constructor (specifying LazyThreadSafetyMode::None for mode). To switch to that 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 [M:System.Lazy`1.#ctor(System.Func`1)] constructor.

The example defines a LargeObject class that will be initialized lazily by one of several threads. The three key sections of code illustrate the creation of the initializer, the actual initialization, and the constructor of the LargeObject class, which demonstrates exception caching. At the beginning of the Main method, the example creates the thread-safe lazy initializer for LargeObject:

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

In the call to the constructor, the isThreadSafe parameter is false, so the Lazy<T> is not thread safe. Because it's not thread safe, the example calls the Value property three times on the same thread:

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

In the constructor of the LargeObject class, the third key section of code throws an exception the first time a LargeObject instance is created, but thereafter allows instance creation to occur:

When the example is run, the first attempt to create an instance of LargeObject fails, and the exception is caught. You might expect that the next attempt would succeed, but the Lazy<T> object has cached the exception. Because of this, all three attempts throw the exception.

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