ThreadLocal<T> 建構函式

定義

初始化 ThreadLocal<T> 執行個體。Initializes a ThreadLocal<T> instance.

多載

ThreadLocal<T>()

初始化 ThreadLocal<T> 執行個體。Initializes the ThreadLocal<T> instance.

ThreadLocal<T>(Boolean)

初始化 ThreadLocal<T> 執行個體,並指定是否可從任何執行緒存取所有值。Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.

ThreadLocal<T>(Func<T>)

使用指定的 valueFactory 函式來初始化 ThreadLocal<T> 的執行個體。Initializes the ThreadLocal<T> instance with the specified valueFactory function.

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

使用指定的 valueFactory 函式及指出是否可從任何執行緒存取所有值的旗標,初始化 ThreadLocal<T> 執行個體。Initializes the ThreadLocal<T> instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread.

ThreadLocal<T>()

初始化 ThreadLocal<T> 執行個體。Initializes the ThreadLocal<T> instance.

public:
 ThreadLocal();
public ThreadLocal ();
Public Sub New ()

備註

當第一次存取 Value 時,會使用 T 的預設值來初始化實例。The default value of T is used to initialize the instance when Value is accessed for the first time.

此函式相當於針對 trackAllValues 引數呼叫 false 值的 ThreadLocal<T>(Boolean) 函數。This constructor is equivalent to calling the ThreadLocal<T>(Boolean) constructor with a value of false for the trackAllValues argument.

另請參閱

ThreadLocal<T>(Boolean)

初始化 ThreadLocal<T> 執行個體,並指定是否可從任何執行緒存取所有值。Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.

public:
 ThreadLocal(bool trackAllValues);
public ThreadLocal (bool trackAllValues);
new System.Threading.ThreadLocal<'T> : bool -> System.Threading.ThreadLocal<'T>
Public Sub New (trackAllValues As Boolean)

參數

trackAllValues
Boolean

true 表示追蹤所有在執行個體上設定的值,並透過 Values 屬性將它們公開;否則為 falsetrue to track all values set on the instance and expose them through the Values property; false otherwise.

備註

如果 trackAllValuesfalse,就只能存取目前線程的這個實例的值。If trackAllValues is false, only the value of this instance for the current thread is accessible. 嘗試使用 Values 屬性來抓取所有值,會擲回 InvalidOperationException 例外狀況。Attempting to use the Values property to retrieve all values throws an InvalidOperationException exception.

ThreadLocal<T>(Func<T>)

使用指定的 valueFactory 函式來初始化 ThreadLocal<T> 的執行個體。Initializes the ThreadLocal<T> instance with the specified valueFactory function.

public:
 ThreadLocal(Func<T> ^ valueFactory);
public ThreadLocal (Func<T> valueFactory);
new System.Threading.ThreadLocal<'T> : Func<'T> -> System.Threading.ThreadLocal<'T>
Public Sub New (valueFactory As Func(Of T))

參數

valueFactory
Func<T>

當嘗試擷取未事先初始化的 Func<TResult> 時,系統會叫用 Value 來產生延遲初始化的值。The Func<TResult> invoked to produce a lazily-initialized value when an attempt is made to retrieve Value without it having been previously initialized.

例外狀況

valueFactory 是 null 參考 (在 Visual Basic 中為 Nothing)。valueFactory is a null reference (Nothing in Visual Basic).

另請參閱

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

使用指定的 valueFactory 函式及指出是否可從任何執行緒存取所有值的旗標,初始化 ThreadLocal<T> 執行個體。Initializes the ThreadLocal<T> instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread.

public:
 ThreadLocal(Func<T> ^ valueFactory, bool trackAllValues);
public ThreadLocal (Func<T> valueFactory, bool trackAllValues);
new System.Threading.ThreadLocal<'T> : Func<'T> * bool -> System.Threading.ThreadLocal<'T>
Public Sub New (valueFactory As Func(Of T), trackAllValues As Boolean)

參數

valueFactory
Func<T>

當嘗試擷取未事先初始化的 Value 時,系統會叫用 Func<TResult> 來產生延遲初始化的值。The Func<TResult> invoked to produce a lazily-initialized value when an attempt is made to retrieve Value without it having been previously initialized.

trackAllValues
Boolean

true 表示追蹤所有在執行個體上設定的值,並透過 Values 屬性將它們公開;否則為 falsetrue to track all values set on the instance and expose them through the Values property; false otherwise.

例外狀況

valueFactorynull 參考 (在 Visual Basic 中為 Nothing)。valueFactory is a null reference (Nothing in Visual Basic).

備註

如果 trackAllValuesfalse,就只能存取目前線程的這個實例的值。If trackAllValues is false, only the value of this instance for the current thread is accessible. 嘗試使用 Values 屬性來抓取所有值,會擲回 InvalidOperationException 例外狀況。Attempting to use the Values property to retrieve all values throws an InvalidOperationException exception.

適用於