开发者

Must static method/classes/variables be accessed one at a time in .net?

I write a lot of my code i开发者_JS百科n static methods/classes/variables so that they can be accessed across the site, for globalization ideas and to "spare" the creation and destruction of classes when they are not data preservation classes(dbcontext for example).

The question is, must these classes/methods be accessed only once at a time? Do they cause any kind of bottle-neck?

Thanks.


The question is does this classes/methods can be accessed only once at a time?

No, they can be accessed multiple times and in parallel. You should be careful with static classes in multi-threaded applications as you need to ensure that they are thread safe.

Does they cause any kind of bottle neck?

This will depend on how they are written and what they do.


Static methods, fields, and properties can be accessed in parallel. If you need to "one at a time", for data safety, you'll need a lock or some other synchronization method.


Not sure what you mean here, but static members are accessed sequentially from any given thread, the same way instance members are.

If you access a static member from multiple threads, then you should synchronize access, i.e. make sure they happen in sequence, by using any of the synchronization constructs available in .NET. This is to avoid unpredictable side effects due to race conditions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜