Lire en anglais Modifier

Partager via


ThreadLocal<T> Constructors

Definition

Initializes a ThreadLocal<T> instance.

Overloads

ThreadLocal<T>()

Initializes the ThreadLocal<T> instance.

ThreadLocal<T>(Boolean)

Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.

ThreadLocal<T>(Func<T>)

Initializes the ThreadLocal<T> instance with the specified valueFactory function.

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

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

Source:
ThreadLocal.cs
Source:
ThreadLocal.cs
Source:
ThreadLocal.cs

Initializes the ThreadLocal<T> instance.

C#
public ThreadLocal();

Remarks

The default value of T is used to initialize the instance when Value is accessed for the first time.

This constructor is equivalent to calling the ThreadLocal<T>(Boolean) constructor with a value of false for the trackAllValues argument.

See also

Applies to

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ThreadLocal<T>(Boolean)

Source:
ThreadLocal.cs
Source:
ThreadLocal.cs
Source:
ThreadLocal.cs

Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.

C#
public ThreadLocal(bool trackAllValues);

Parameters

trackAllValues
Boolean

true to track all values set on the instance and expose them through the Values property; false otherwise. When set to true, a value stored from a given thread will be available through Values even after that thread has exited.

Remarks

If trackAllValues is false, only the value of this instance for the current thread is accessible. Attempting to use the Values property to retrieve all values throws an InvalidOperationException exception.

Applies to

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ThreadLocal<T>(Func<T>)

Source:
ThreadLocal.cs
Source:
ThreadLocal.cs
Source:
ThreadLocal.cs

Initializes the ThreadLocal<T> instance with the specified valueFactory function.

C#
public ThreadLocal(Func<T> valueFactory);

Parameters

valueFactory
Func<T>

The Func<TResult> invoked to produce a lazily-initialized value when an attempt is made to retrieve Value without it having been previously initialized.

Exceptions

valueFactory is a null reference (Nothing in Visual Basic).

See also

Applies to

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
ThreadLocal.cs
Source:
ThreadLocal.cs
Source:
ThreadLocal.cs

Initializes the ThreadLocal<T> instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread.

C#
public ThreadLocal(Func<T> valueFactory, bool trackAllValues);

Parameters

valueFactory
Func<T>

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 to track all values set on the instance and expose them through the Values property; false otherwise. When set to true, a value stored from a given thread will be available through Values even after that thread has exited.

Exceptions

valueFactory is a null reference (Nothing in Visual Basic).

Remarks

If trackAllValues is false, only the value of this instance for the current thread is accessible. Attempting to use the Values property to retrieve all values throws an InvalidOperationException exception.

Applies to

.NET 10 et autres versions
Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0