ThreadStatic Modified with Static C#

自作多情 提交于 2019-11-30 11:17:23

问题


I have some code where I use a thread static object in C#.

[ThreadStatic]
private DataContext connection 

I was wondering, in this case, what if any change would I get if I put the static modifier on the thread static context?

[ThreadStatic]
private static DataContext connection 

With the first would there be one copy of the context per instance per thread, with the other only one copy per thread?


回答1:


The ThreadStaticAttribute is only designed to be used on static variables, as MSDN points out. If you use it on an instance variable, I suspect it will do precisely nothing.




回答2:


In the first case it would probably be ignored, whereas in the second case you are correct, one instance per thread.




回答3:


MSDN says :

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

So I guess you first case is incorrect... the attribute will probably be ignored



来源:https://stackoverflow.com/questions/868537/threadstatic-modified-with-static-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!