John is right; in fact, itβs not clear what you are asking here. I would interpret your question:
If I have some general state that needs to be made thread safe by blocking it, do I need to declare a closed static object as a lock object?
The answer to this question is no, you are not required to do this. However, this is a really good idea, so you should do it even if you do not need it.
You might think that there are many objects that I could use. If the object I'm locking is a reference type, I could use it. Or I could use the Type object associated with the containing class.
The problem with these things as locks is that it becomes difficult to keep track of all the possible bits of code that can use this thing as a lock. Therefore, it becomes difficult to analyze the code to make sure that there are no deadlocks due to problems with locking ordering. And so you have a much better chance of getting deadlocks. Having a dedicated lock object makes it a lot easier; you know that every use of this object is intended to be locked, and you can understand what happens inside these locks.
This is especially true if you have ever had untrustworthy, hostile code running in your application. Locking an object of a type does not require special permissions; What stops the hostile code from blocking all types and never unlocks them? Nothing, that's what.
source share