Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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> or Lazy<T> 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> 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.


using System;
using System.Threading;

class Program
{
    static Lazy<LargeObject> lazyLargeObject = null;

    static void Main()
    {
        // The lazy initializer is created here. LargeObject is not created until the 
        // ThreadProc method executes.
        lazyLargeObject = new Lazy<LargeObject>(false);

        // The following lines show how to use other constructors to achieve exactly the
        // same result as the previous line: 
        //lazyLargeObject = new Lazy<LargeObject>(LazyThreadSafetyMode.None);


        Console.WriteLine(
            "\r\nLargeObject is not created until you access the Value property of the lazy" +
            "\r\ninitializer. Press Enter to create LargeObject.");
        Console.ReadLine();

        LargeObject large = lazyLargeObject.Value;

        large.Data[11] = 89;

        Console.WriteLine("\r\nPress Enter to end the program");
        Console.ReadLine();
    }
}

class LargeObject
{
    public LargeObject()
    {
        Console.WriteLine("LargeObject was created on thread id {0}.", 
            Thread.CurrentThread.ManagedThreadId);
    }
    public long[] Data = new long[100000000];
}

/* This example produces output similar to the following:

LargeObject is not created until you access the Value property of the lazy
initializer. Press Enter to create LargeObject.

LargeObject was created on thread id 1.

Press Enter to end the program
 */


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.