regarding [ThreadStatic()] c#
what is the meaning of [ThreadStatic()]
i got a piece of code and i found [ThreadStatic()]
is used there what does it mean....when to use [ThreadStatic()]
public class Contex开发者_开发问答t
{
[ThreadStatic()]
private static Context _Context = null;
private HttpContext _HttpContext = null;
public Context()
{
_HttpContext = HttpContext.Current;
}
public static Context Current
{
if(_Context == null ||
_HttpContext != _HttpContext.Current)
{
_Context = new Context();
}
return _Context;
}
}
From MSDN:
Indicates that the value of a static field is unique for each thread.
Read these:
- A tale of two techniques: The [ThreadStatic] Attribute and System.Web.HttpContext.Current.Items
- CallContext vs ThreadStatic
- MSDN Blog: Are you familiar with [ThreadStatic]?
- How well do you understand ThreadStatic?
From the documentation:
Indicates that the value of a static field is unique for each thread.
In your code _Context
is static, but it is different for each thread.
If you have a background in more native programming, think of these as a semi-equivalent of Thread Local Storage.
精彩评论