ThreadLocal<T> Class

July 28, 2014

Provides thread-local storage of data.

System.Object
  System.Threading.ThreadLocal<T>

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public class ThreadLocal<T> : IDisposable

Type Parameters

T

Specifies the type of data stored per-thread.

The ThreadLocal<T> type exposes the following members.

  NameDescription
Public methodThreadLocal<T>()Initializes the ThreadLocal<T> instance.
Public methodThreadLocal<T>(Boolean)Initializes the ThreadLocal<T> instance.
Public methodThreadLocal<T>(Func<T>)Initializes the ThreadLocal<T> instance with the specified valueFactory function.
Public methodThreadLocal<T>(Func<T>, Boolean)Initializes the ThreadLocal<T> instance with the specified valueFactory function.
Top

  NameDescription
Public propertyIsValueCreatedGets whether Value is initialized on the current thread.
Public propertyValueGets or sets the value of this instance for the current thread.
Public propertyValuesGets a list for all of the values currently stored by all of the threads that have accessed this instance.
Top

  NameDescription
Public methodDispose()Releases all resources used by the current instance of the ThreadLocal<T> class.
Protected methodDispose(Boolean)Releases the resources used by this ThreadLocal<T> instance.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeReleases the resources used by this ThreadLocal<T> instance. (Overrides Object.Finalize().)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringCreates and returns a string representation of this instance for the current thread. (Overrides Object.ToString().)
Top

The following example shows how to use ThreadLocal<T>:

Windows Phone OS

Supported in: 8.1, 8.0

With the exception of Dispose, all public and protected members of ThreadLocal<T> thread-safe and may be used concurrently from multiple threads. The value returned for the Value and IsValueCreated properties is specific for the thread on which the property is accessed.

Show:
© 2014 Microsoft