Lazy<T> Lazy<T> Lazy<T> Lazy<T> Class

定義

提供延遲初始設定的支援。Provides support for lazy initialization.

generic <typename T>
public ref class Lazy
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Lazy<T>
type Lazy<'T> = class
Public Class Lazy(Of T)

類型參數

T

正在延遲初始化的物件類型。The type of object that is being lazily initialized.

繼承
Lazy<T>Lazy<T>Lazy<T>Lazy<T>
衍生
屬性

範例

下列範例示範如何使用Lazy<T>類別,以提供從多個執行緒存取的延遲初始設定。The following example demonstrates the use of the Lazy<T> class to provide lazy initialization with access from multiple threads.

注意

此範例會使用Lazy<T>(Func<T>)建構函式。The example uses the Lazy<T>(Func<T>) constructor. 它也示範如何使用Lazy<T>(Func<T>, Boolean)建構函式 (指定truefor isThreadSafe) 和Lazy<T>(Func<T>, LazyThreadSafetyMode)建構函式 (指定LazyThreadSafetyMode.ExecutionAndPublicationmode)。It also demonstrates the use of the Lazy<T>(Func<T>, Boolean) constructor (specifying true for isThreadSafe) and the Lazy<T>(Func<T>, LazyThreadSafetyMode) constructor (specifying LazyThreadSafetyMode.ExecutionAndPublication for mode). 若要切換至不同的建構函式,只需變更的建構函式標記為註解。To switch to a different constructor, just change which constructors are commented out.

如需示範例外狀況的範例快取使用相同的建構函式,請參閱Lazy<T>(Func<T>)建構函式。For an example that demonstrates exception caching using the same constructors, see the Lazy<T>(Func<T>) constructor.

這個範例定義將由多個執行緒中的一個執行延遲初始化的 LargeObject 類別。The example defines a LargeObject class that will be initialized lazily by one of several threads. 程式碼的四個金鑰各節將說明建立初始設定式、 factory 方法,實際的初始化和的建構函式LargeObject類別,建立物件時,會顯示訊息。The four key sections of code illustrate the creation of the initializer, the factory method, the actual initialization, and the constructor of the LargeObject class, which displays a message when the object is created. Main 方法的開頭,此範例會為 LargeObject 建立安全執行緒延遲初始設定式:At the beginning of the Main method, the example creates the thread-safe lazy initializer for LargeObject:

lazyLargeObject = new Lazy<LargeObject>(InitLargeObject);

// The following lines show how to use other constructors to achieve exactly the
// same result as the previous line: 
//lazyLargeObject = new Lazy<LargeObject>(InitLargeObject, true);
//lazyLargeObject = new Lazy<LargeObject>(InitLargeObject, 
//                               LazyThreadSafetyMode.ExecutionAndPublication);
lazyLargeObject = New Lazy(Of LargeObject)(AddressOf InitLargeObject)

' The following lines show how to use other constructors to achieve exactly the
' same result as the previous line: 
'lazyLargeObject = new Lazy<LargeObject>(InitLargeObject, true);
'lazyLargeObject = new Lazy<LargeObject>(InitLargeObject, _
'                               LazyThreadSafetyMode.ExecutionAndPublication);

Factory 方法顯示如何建立物件,以進行進一步的初始化預留位置:The factory method shows the creation of the object, with a placeholder for further initialization:

static LargeObject InitLargeObject()
{
    LargeObject large = new LargeObject(Thread.CurrentThread.ManagedThreadId);
    // Perform additional initialization here.
    return large;
}
Private Shared Function InitLargeObject() As LargeObject
    Dim large As New LargeObject(Thread.CurrentThread.ManagedThreadId)
    ' Perform additional initialization here.
    Return large
End Function

請注意,使用 lambda 函式,無法合併前, 兩個程式碼區段,如下所示:Note that the first two code sections could be combined by using a lambda function, as shown here:

lazyLargeObject = new Lazy<LargeObject>(() => 
{
    LargeObject large = new LargeObject(Thread.CurrentThread.ManagedThreadId);
    // Perform additional initialization here.
    return large;
});
lazyLargeObject = New Lazy(Of LargeObject)(Function () 
    Dim large As New LargeObject(Thread.CurrentThread.ManagedThreadId) 
    ' Perform additional initialization here.
    Return large
End Function)

此範例會暫停,表示沒有期限在延遲初始設定之前,可能需要經過之後。The example pauses, to indicate that an indeterminate period may elapse before lazy initialization occurs. 當您按下Enter索引鍵,此範例會建立並啟動三個執行緒。When you press the Enter key, the example creates and starts three threads. ThreadProc方法,可由所有三個執行緒呼叫Value屬性。The ThreadProc method that's used by all three threads calls the Value property. 發生這種情況,第一次LargeObject建立執行個體:The first time this happens, the LargeObject instance is created:

LargeObject large = lazyLargeObject.Value;

// IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the  
//            object after creation. You must lock the object before accessing it,
//            unless the type is thread safe. (LargeObject is not thread safe.)
lock(large)
{
    large.Data[0] = Thread.CurrentThread.ManagedThreadId;
    Console.WriteLine("Initialized by thread {0}; last used by thread {1}.", 
        large.InitializedBy, large.Data[0]);
}
Dim large As LargeObject = lazyLargeObject.Value

' IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the  
'            object after creation. You must lock the object before accessing it,
'            unless the type is thread safe. (LargeObject is not thread safe.)
SyncLock large
    large.Data(0) = Thread.CurrentThread.ManagedThreadId
    Console.WriteLine("Initialized by thread {0}; last used by thread {1}.", _
        large.InitializedBy, large.Data(0))
End SyncLock

建構函式LargeObject類別,其中包含程式碼的最後一節,顯示一則訊息,並記錄初始化執行緒的身分識別。The constructor of the LargeObject class, which includes the last key section of code, displays a message and records the identity of the initializing thread. 程式的輸出會顯示完整的程式碼清單的結尾。The output from the program appears at the end of the full code listing.

int initBy = 0;
public LargeObject(int initializedBy)
{
    initBy = initializedBy;
    Console.WriteLine("LargeObject was created on thread id {0}.", initBy);
}
Private initBy As Integer = 0
Public Sub New(ByVal initializedBy As Integer)
    initBy = initializedBy
    Console.WriteLine("LargeObject was created on thread id {0}.", initBy)
End Sub

注意

為了簡化,這個範例使用 Lazy<T> 的全域執行個體,而且所有方法都是 static (Visual Basic 中為 Shared)。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 LargeObject InitLargeObject()
    {
        LargeObject large = new LargeObject(Thread.CurrentThread.ManagedThreadId);
        // Perform additional initialization here.
        return large;
    }
    

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

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


        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();

        // Create and start 3 threads, each of which uses LargeObject.
        Thread[] threads = new Thread[3];
        for (int i = 0; i < 3; i++)
        {
            threads[i] = new Thread(ThreadProc);
            threads[i].Start();
        }

        // Wait for all 3 threads to finish. 
        foreach (Thread t in threads)
        {
            t.Join();
        }

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


    static void ThreadProc(object state)
    {
        LargeObject large = lazyLargeObject.Value;

        // IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the  
        //            object after creation. You must lock the object before accessing it,
        //            unless the type is thread safe. (LargeObject is not thread safe.)
        lock(large)
        {
            large.Data[0] = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("Initialized by thread {0}; last used by thread {1}.", 
                large.InitializedBy, large.Data[0]);
        }
    }
}

class LargeObject
{
    public int InitializedBy { get { return initBy; } }

    int initBy = 0;
    public LargeObject(int initializedBy)
    {
        initBy = initializedBy;
        Console.WriteLine("LargeObject was created on thread id {0}.", initBy);
    }

    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 3.
Initialized by thread 3; last used by thread 3.
Initialized by thread 3; last used by thread 4.
Initialized by thread 3; last used by thread 5.

Press Enter to end the program
 */
Imports System
Imports System.Threading

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

    Private Shared Function InitLargeObject() As LargeObject
        Dim large As New LargeObject(Thread.CurrentThread.ManagedThreadId)
        ' Perform additional initialization here.
        Return large
    End Function


    Shared Sub Main()
        ' The lazy initializer is created here. LargeObject is not created until the 
        ' ThreadProc method executes.
        lazyLargeObject = New Lazy(Of LargeObject)(AddressOf InitLargeObject)

        ' The following lines show how to use other constructors to achieve exactly the
        ' same result as the previous line: 
        'lazyLargeObject = new Lazy<LargeObject>(InitLargeObject, true);
        'lazyLargeObject = new Lazy<LargeObject>(InitLargeObject, _
        '                               LazyThreadSafetyMode.ExecutionAndPublication);


        Console.WriteLine(vbCrLf & _
            "LargeObject is not created until you access the Value property of the lazy" _
            & vbCrLf & "initializer. Press Enter to create LargeObject.")
        Console.ReadLine()

        ' Create and start 3 threads, each of which uses LargeObject.
        Dim threads(2) As Thread
        For i As Integer = 0 To 2
            threads(i) = New Thread(AddressOf ThreadProc)
            threads(i).Start()
        Next i

        ' Wait for all 3 threads to finish. 
        For Each t As Thread In threads
            t.Join()
        Next t

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


    Private Shared Sub ThreadProc(ByVal state As Object)
        Dim large As LargeObject = lazyLargeObject.Value

        ' IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the  
        '            object after creation. You must lock the object before accessing it,
        '            unless the type is thread safe. (LargeObject is not thread safe.)
        SyncLock large
            large.Data(0) = Thread.CurrentThread.ManagedThreadId
            Console.WriteLine("Initialized by thread {0}; last used by thread {1}.", _
                large.InitializedBy, large.Data(0))
        End SyncLock
    End Sub
End Class

Friend Class LargeObject
    Public ReadOnly Property InitializedBy() As Integer
        Get
            Return initBy
        End Get
    End Property

    Private initBy As Integer = 0
    Public Sub New(ByVal initializedBy As Integer)
        initBy = initializedBy
        Console.WriteLine("LargeObject was created on thread id {0}.", initBy)
    End Sub

    Public Data(99999999) 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 3.
'Initialized by thread 3; last used by thread 3.
'Initialized by thread 3; last used by thread 5.
'Initialized by thread 3; last used by thread 4.
'
'Press Enter to end the program
' 

備註

延後建立大型或需要大量資源的物件或執行需要大量資源的工作,尤其是這類的建立或執行可能不會發生在程式的存留期間使用延遲初始設定。Use lazy initialization to defer the creation of a large or resource-intensive object, or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program.

若要準備延遲初始設定,您建立的執行個體Lazy<T>To prepare for lazy initialization, you create an instance of Lazy<T>. 類型引數Lazy<T>您所建立的物件會指定您想要延遲初始化的物件型別。The type argument of the Lazy<T> object that you create specifies the type of the object that you want to initialize lazily. 您用來建立建構函式Lazy<T>物件會決定初始化的特性。The constructor that you use to create the Lazy<T> object determines the characteristics of the initialization. 延遲初始化會在第一次存取 Lazy<T>.Value 屬性時發生。Lazy initialization occurs the first time the Lazy<T>.Value property is accessed.

在大部分情況下,選擇一個建構函式取決於您對兩個問題的答案:In most cases, choosing a constructor depends on your answers to two questions:

  • 會從多個執行緒存取延遲初始化的物件嗎?Will the lazily initialized object be accessed from more than one thread? 如果是這樣,Lazy<T>物件可能會在任何執行緒上建立它。If so, the Lazy<T> object might create it on any thread. 您可以使用其中一個簡單的建構函式的預設行為是建立安全執行緒Lazy<T>物件,因此無論多少個執行緒嘗試存取它建立延遲具現化物件的該只有一個執行個體。You can use one of the simple constructors whose default behavior is to create a thread-safe Lazy<T> object, so that only one instance of the lazily instantiated object is created no matter how many threads try to access it. 若要建立Lazy<T>不是安全執行緒的物件,您必須使用可讓您不指定任何執行緒安全的建構函式。To create a Lazy<T> object that is not thread safe, you must use a constructor that enables you to specify no thread safety.

    警告

    Lazy<T>物件具備執行緒安全,並無法保護延遲初始化的物件。Making the Lazy<T> object thread safe does not protect the lazily initialized object. 如果多個執行緒可以存取的延遲初始化的物件,您必須使其屬性和方法進行多執行緒存取的安全。If multiple threads can access the lazily initialized object, you must make its properties and methods safe for multithreaded access.

  • 延遲初始設定是否需要大量程式碼,或延遲初始化的物件是否有預設建構函式不需要而且不會擲回例外狀況的所有內容?Does lazy initialization require a lot of code, or does the lazily initialized object have a default constructor that does everything you need and doesn't throw exceptions? 如果您要撰寫初始化程式碼,或需要處理的例外狀況,請使用其中一個會採用的 factory 方法的建構函式。If you need to write initialization code or if exceptions need to be handled, use one of the constructors that takes a factory method. Factory 方法中撰寫您的初始化程式碼。Write your initialization code in the factory method.

下表顯示的建構函式,若要選擇,根據這兩個因素:The following table shows which constructor to choose, based on these two factors:

物件會由存取Object will be accessed by 如果不不需要任何初始設定程式碼 (預設建構函式),使用If no initialization code is required (default constructor), use 如果需要初始化程式碼,使用If initialization code is required, use
多個執行緒Multiple threads Lazy<T>() Lazy<T>(Func<T>)
一個執行緒One thread Lazy<T>(Boolean) 具有isThreadSafe設定為falseLazy<T>(Boolean) with isThreadSafe set to false. Lazy<T>(Func<T>, Boolean) 具有isThreadSafe設定為falseLazy<T>(Func<T>, Boolean) with isThreadSafe set to false.

您可以使用 lambda 運算式來指定的 factory 方法。You can use a lambda expression to specify the factory method. 這會在一個位置中保留所有初始化程式碼。This keeps all the initialization code in one place. Lambda 運算式會擷取的內容,包括任何您傳遞給延遲初始化的物件建構函式的引數。The lambda expression captures the context, including any arguments you pass to the lazily initialized object's constructor.

例外狀況快取當您使用 factory 方法時,會快取例外狀況。Exception caching When you use factory methods, exceptions are cached. 也就是說,如果的 factory 方法,就會擲回的例外狀況的第一個時間的執行緒嘗試存取Value屬性Lazy<T>物件,每個後續的嘗試都會擲回相同的例外狀況。That is, if the factory method throws an exception the first time a thread tries to access the Value property of the Lazy<T> object, the same exception is thrown on every subsequent attempt. 這可確保每次呼叫Value屬性會產生相同的結果,並避免不同的執行緒取得不同的結果可能會引起的細微錯誤。This ensures that every call to the Value property produces the same result and avoids subtle errors that might arise if different threads get different results. Lazy<T>代表實際T,否則會初始化後在某個較早的時間點,通常會在啟動期間。The Lazy<T> stands in for an actual T that otherwise would have been initialized at some earlier point, usually during startup. 在該較早的時間點失敗是通常嚴重。A failure at that earlier point is usually fatal. 如果沒有可復原的失敗可能,建議您 (在此情況下,處理站方法),在初始化常式到建置重試邏輯,就像您一樣如果您未使用延遲初始設定。If there is a potential for a recoverable failure, we recommend that you build the retry logic into the initialization routine (in this case, the factory method), just as you would if you weren't using lazy initialization.

鎖定的替代方法在某些情況下,您可能想要避免的額外負荷Lazy<T>物件的預設鎖定行為。Alternative to locking In certain situations, you might want to avoid the overhead of the Lazy<T> object's default locking behavior. 在罕見的情況下,可能會有可能會有死結。In rare situations, there might be a potential for deadlocks. 在這種情況下,您可以使用Lazy<T>(LazyThreadSafetyMode)或是Lazy<T>(Func<T>, LazyThreadSafetyMode)建構函式,並指定LazyThreadSafetyMode.PublicationOnlyIn such cases, you can use the Lazy<T>(LazyThreadSafetyMode) or Lazy<T>(Func<T>, LazyThreadSafetyMode) constructor, and specify LazyThreadSafetyMode.PublicationOnly. 這可讓Lazy<T>物件,以在每個有數個執行緒上建立一份延遲初始化的物件,如果執行緒呼叫Value屬性同時。This enables the Lazy<T> object to create a copy of the lazily initialized object on each of several threads if the threads call the Value property simultaneously. Lazy<T>物件可確保所有執行緒使用相同的執行個體的延遲初始化的物件,並捨棄未使用的執行個體。The Lazy<T> object ensures that all threads use the same instance of the lazily initialized object and discards the instances that are not used. 因此,減少鎖定負擔的成本是物件的您的程式可能有時建立和捨棄的高度耗費資源的額外複本。Thus, the cost of reducing the locking overhead is that your program might sometimes create and discard extra copies of an expensive object. 在大部分情況下,這是不太可能項目。In most cases, this is unlikely. 範例Lazy<T>(LazyThreadSafetyMode)Lazy<T>(Func<T>, LazyThreadSafetyMode)建構函式都會表現此行為。The examples for the Lazy<T>(LazyThreadSafetyMode) and Lazy<T>(Func<T>, LazyThreadSafetyMode) constructors demonstrate this behavior.

重要

當您指定LazyThreadSafetyMode.PublicationOnly,例外狀況會永遠不會快取,即使您指定的 factory 方法。When you specify LazyThreadSafetyMode.PublicationOnly, exceptions are never cached, even if you specify a factory method.

對等的建構函式除了會啟用LazyThreadSafetyMode.PublicationOnly,則Lazy<T>(LazyThreadSafetyMode)Lazy<T>(Func<T>, LazyThreadSafetyMode)建構函式可以複製的其他建構函式的功能。Equivalent constructors In addition to enabling the use of LazyThreadSafetyMode.PublicationOnly, the Lazy<T>(LazyThreadSafetyMode) and Lazy<T>(Func<T>, LazyThreadSafetyMode) constructors can duplicate the functionality of the other constructors. 下表顯示參數值會產生對等的行為。The following table shows the parameter values that produce equivalent behavior.

若要建立Lazy<T>物件To create a Lazy<T> object that is 針對具有建構函式LazyThreadSafetyMode``mode參數,設定modeFor constructors that have a LazyThreadSafetyMode mode parameter, set mode to 針對具有布林值的建構函式isThreadSafe參數,設定isThreadSafeFor constructors that have a Boolean isThreadSafe parameter, set isThreadSafe to 針對沒有執行緒安全性參數的建構函式For constructors with no thread safety parameters
完全具備執行緒安全;會使用鎖定來確保只有一個執行緒初始化值。Fully thread safe; uses locking to ensure that only one thread initializes the value. ExecutionAndPublication true 所有這類建構函式都完全是安全執行緒。All such constructors are fully thread safe.
不具備執行緒安全。Not thread safe. None false 不適用。Not applicable.
完全具備執行緒安全;執行緒競爭,來初始化值。Fully thread safe; threads race to initialize the value. PublicationOnly 不適用。Not applicable. 不適用。Not applicable.

其他功能如需有關使用資訊Lazy<T>執行緒靜態欄位或屬性的備份存放區,請參閱延遲初始設定Other capabilities For information about the use of Lazy<T> with thread-static fields, or as the backing store for properties, see Lazy Initialization.

建構函式

Lazy<T>() Lazy<T>() Lazy<T>() Lazy<T>()

初始化 Lazy<T> 類別的新執行個體。Initializes a new instance of the Lazy<T> class. 發生延遲初始設定時,會使用目標類型的預設建構函式。When lazy initialization occurs, the default constructor of the target type is used.

Lazy<T>(Boolean) Lazy<T>(Boolean) Lazy<T>(Boolean) Lazy<T>(Boolean)

初始化 Lazy<T> 類別的新執行個體。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.

Lazy<T>(Func<T>) Lazy<T>(Func<T>) Lazy<T>(Func<T>) Lazy<T>(Func<T>)

初始化 Lazy<T> 類別的新執行個體。Initializes a new instance of the Lazy<T> class. 當延遲初始設定發生時,就會使用指定的初始化函式。When lazy initialization occurs, the specified initialization function is used.

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

初始化 Lazy<T> 類別的新執行個體。Initializes a new instance of the Lazy<T> class. 當延遲初始設定發生時,會使用指定的初始化函式和初始化模式。When lazy initialization occurs, the specified initialization function and initialization mode are used.

Lazy<T>(Func<T>, LazyThreadSafetyMode) Lazy<T>(Func<T>, LazyThreadSafetyMode) Lazy<T>(Func<T>, LazyThreadSafetyMode) Lazy<T>(Func<T>, LazyThreadSafetyMode)

初始化 Lazy<T> 類別的新執行個體,此類別使用的是指定的初始化函數和執行緒安全模式。Initializes a new instance of the Lazy<T> class that uses the specified initialization function and thread-safety mode.

Lazy<T>(LazyThreadSafetyMode) Lazy<T>(LazyThreadSafetyMode) Lazy<T>(LazyThreadSafetyMode) Lazy<T>(LazyThreadSafetyMode)

初始化 Lazy<T> 類別的新執行個體,這些類別會使用 T 的預設建構函式和指定執行緒安全模式。Initializes a new instance of the Lazy<T> class that uses the default constructor of T and the specified thread-safety mode.

Lazy<T>(T) Lazy<T>(T) Lazy<T>(T) Lazy<T>(T)

屬性

IsValueCreated IsValueCreated IsValueCreated IsValueCreated

取得值,指出某個值是否已經建立這個 Lazy<T> 執行個體。Gets a value that indicates whether a value has been created for this Lazy<T> instance.

Value Value Value Value

取得目前 Lazy<T> 執行個體的延遲初始化值。Gets the lazily initialized value of the current Lazy<T> instance.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

建立並傳回這個執行個體之 Value 屬性的字串表示。Creates and returns a string representation of the Value property for this instance.

適用於

執行緒安全性

根據預設,所有的 public 和 protected 成員的Lazy<T>類別都是安全執行緒,且可能會從多個執行緒同時使用。By default, all public and protected members of the Lazy<T> class are thread safe and may be used concurrently from multiple threads. (選擇性) 和每個執行個體,使用此類型的建構函式的參數,可能會移除這些執行緒安全性保證。These thread-safety guarantees may be removed optionally and per instance, using parameters to the type's constructors.

另請參閱