what is the reason??
Because anything that is not private means that could be used from the outside to lock on by someone else or some code that is outside from your control leading to deadlocks.
The best practice is to lock on private static variables, like this:
private static object _syncRoot = new object();
and then:
lock(_syncRoot)
{
...
}
private instance variables could also be dangerous since the instance of your class is not something that you as implementer of the class own. It's the consumer of the class that owns the instance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…