TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  6@119

Synopsis:Avoid locking on a public type
Language:C#
Severity Level:4
Category:Control flow


Description:

In general, avoid locking on a public type, or instances beyond your code's control. The common constructs "lock (this)", "lock (typeof (MyType))", and "lock ("myLock")" violate this guideline:

  • "lock (this)" is a problem if the instance can be accessed publicly.
  • "lock (typeof (MyType))" is a problem if MyType is publicly accessible.
  • "lock("myLock")" is a problem because any other code in the process using the same string, will share the same lock.

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.



Literature References:
MS Design