Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
.NET Framework
System Namespace
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ThreadStaticAttribute Class

Indicates that the value of a static field is unique for each thread.

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

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Field, Inherited := False)> _
Public Class ThreadStaticAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As ThreadStaticAttribute
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)]
public class ThreadStaticAttribute : Attribute
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::Field, Inherited = false)]
public ref class ThreadStaticAttribute : public Attribute
J#
/** @attribute SerializableAttribute */ 
/** @attribute ComVisibleAttribute(true) */
/** @attribute AttributeUsageAttribute(AttributeTargets.Field, Inherited = false) */
public class ThreadStaticAttribute extends Attribute
JScript
public class ThreadStaticAttribute extends Attribute

A static field marked with ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value.

Note:

Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to nullNothingnullptra null reference (Nothing in Visual Basic) if it is a reference type.

Use this attribute as it is, and do not derive from it. The following is an example of ThreadStaticAttribute syntax:

 [ThreadStatic]
 static int value;
 <ThreadStatic> _
 Shared value As Integer

For more information about using attributes, see Extending Metadata Using Attributes.

System..::.Object
  System..::.Attribute
    System..::.ThreadStaticAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0 SP1, 3.0, 2.0 SP1, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker