Child pages
  • 'ThreadStaticAttribute' usage
Skip to end of metadata
Go to start of metadata

The ThreadStaticAttribute makes a field thread-local. This means that every thread has its own reference corresponding to the field. Fields marked with ThreadStaticAttribute must be static and not initialized statically.

This attribute does not affect instance fields. If you need a thread-local instance field you can use the ThreadLocal<> type that has been introduced in .NET 4.0.

If a static field has an initializer, this initializer is invoked only once — on the thread that executes the static constructor. If initialization on all threads is needed, then the field can be encapsulated by a lazy-initialized property:

Alternatively, the ThreadLocal<> class can be used (as of .NET 4.0):

  • No labels