C#

C#

Made by DeepSource

Lock is obtained on a public object CS-W1046

Bug risk
Critical

Microsoft guidelines specifically state that locks should not be obtained on this, System.Types, 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 locking 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.

Bad Practice

// lockObj is an `object` that is class' public member.
lock(lockObj)
{
    // ...
}

Recommended

// _lockObj is an `object` that is class' private readonly member.
lock(_lockObj)
{
    // ...
}

Reference