Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the C# language reference <a href="http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx" rel="noreferrer">here</a>:</p> <blockquote> <p>In general, avoid locking on a public type, or instances beyond your code's control. The common constructs <code>lock (this)</code>, <code>lock (typeof (MyType))</code>, and <code>lock ("myLock")</code> violate this guideline:</p> <p><code>lock (this)</code> is a problem if the instance can be accessed publicly.</p> <p><code>lock (typeof (MyType))</code> is a problem if <code>MyType</code> is publicly accessible.</p> <p><code>lock("myLock")</code> is a problem because any other code in the process using the same string, will share the same lock. </p> <p>Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.</p> </blockquote> <p>In your case, I would read the above guidance as suggesting that locking on the collection you will be modifying is bad practise. For example, if you wrote this code:</p> <pre><code>lock (otherProductList) { otherProductList = new List&lt;IProduct&gt;(); } </code></pre> <p>...then your lock will be worthless. For these reasons it's recommended to use a dedicated <code>object</code> variable for locking.</p> <p>Note that this doesn't mean your application will <em>break</em> if you use the code you posted. "Best practices" are usually defined to provide easily-repeated patterns that are more technically resilient. That is, if you follow best practice and have a dedicated "lock object," you are highly unlikely to <em>ever</em> write broken <code>lock</code>-based code; if you don't follow best practice then, maybe one time in a hundred, you'll get bitten by an easily-avoided problem.</p> <p>Additionally (and more generally), code written using best practices is typically more easily modified, because you can be less wary of unexpected side-effects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload