public
object
CS-W1046Microsoft guidelines specifically state that lock
s should not be obtained on this
, System.Type
s, and string
instances. Additionally, it is also recommended that you do not lock on a public
object
as they are mutable and there is no fool-proof way to know who else is lock
ing on the same object
. Doing so may cause deadlock or lock contention, thereby affecting your application's execution and reliability. It is generally recommended that you dedicate a private
readonly
object
solely for locking.
// lockObj is an `object` that is class' public member.
lock(lockObj)
{
// ...
}
// _lockObj is an `object` that is class' private readonly member.
lock(_lockObj)
{
// ...
}