Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ThreadLocal<T> Class

.NET Framework (current version)

Provides thread-local storage of data.

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


[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public class ThreadLocal<T> : IDisposable

Type Parameters


Specifies the type of data stored per-thread.


Initializes the ThreadLocal<T> instance.


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


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

System_CAPS_pubmethodThreadLocal<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.


Gets whether Value is initialized on the current thread.


Gets or sets the value of this instance for the current thread.


Gets a list for all of the values currently stored by all of the threads that have accessed this instance.


Releases all resources used by the current instance of the ThreadLocal<T> class.


Releases the resources used by this ThreadLocal<T> instance.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Releases the resources used by this ThreadLocal<T> instance.(Overrides Object.Finalize().)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Creates and returns a string representation of this instance for the current thread.(Overrides Object.ToString().)

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

using System;
using System.Threading;
using System.Threading.Tasks;

class ThreadLocalDemo

        // Demonstrates:
        //      ThreadLocal(T) constructor
        //      ThreadLocal(T).Value
        //      One usage of ThreadLocal(T)
        static void Main()
            // Thread-Local variable that yields a name for a thread
            ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
                return "Thread" + Thread.CurrentThread.ManagedThreadId;

            // Action that prints out ThreadName for the current thread
            Action action = () =>
                // If ThreadName.IsValueCreated is true, it means that we are not the
                // first action to run on this thread.
                bool repeat = ThreadName.IsValueCreated;

                Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : "");

            // Launch eight of them.  On 4 cores or less, you should see some repeat ThreadNames
            Parallel.Invoke(action, action, action, action, action, action, action, action);

            // Dispose when you are done

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1

With the exception of Dispose, all public and protected members of ThreadLocal<T> are 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.

Return to top
© 2017 Microsoft