Lazy(Of T) Constructor (Boolean)


Initializes a new instance of the Lazy(Of 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 Sub New (
	isThreadSafe As Boolean


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(Of 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(Of T) instance. To specify the LazyThreadSafetyMode.PublicationOnly mode, use the Lazy(Of T)(Func(Of T), LazyThreadSafetyMode) or Lazy(Of T)(LazyThreadSafetyMode) constructor.

A Lazy(Of T) instance that is created with this constructor does not cache exceptions. For more information, see theLazy(Of 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(Of 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(Of T)() constructor.

The example defines a LargeObject class that will be initialized lazily. In the Main method, the example creates a Lazy(Of T) instance and then pauses. When you press the Enter key, the example accesses the Value property of the Lazy(Of 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(Of T), and all the methods are static (Shared in Visual Basic). These are not requirements for the use of lazy initialization.

Imports System
Imports System.Threading

Friend Class Program
    Private Shared lazyLargeObject As Lazy(Of LargeObject) = Nothing

    Shared Sub Main()
        ' The lazy initializer is created here. LargeObject is not created until the 
        ' ThreadProc method executes.
        lazyLargeObject = New Lazy(Of 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( _
            vbCrLf & "LargeObject is not created until you access the Value property of the lazy" _
            & vbCrLf & "initializer. Press Enter to create LargeObject.")

        Dim large As LargeObject = lazyLargeObject.Value

        large.Data(11) = 89

        Console.WriteLine(vbCrLf & "Press Enter to end the program")
    End Sub
End Class

Friend Class LargeObject
    Public Sub New()
        Console.WriteLine("LargeObject was created on thread id {0}.", _
    End Sub
    Public Data(100000000) As Long
End Class

' 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

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