title: Proper way to use SyncLock (in general)
body: <p>This is a follow-up to a <a href="https://stackoverflow.com/questions/4800941/vb-net-do-i-need-multiple-synclocks-if-i-want-to-lock-multiple-things" title="VB.NET Do I need multiple SyncLocks if I want to lock multiple things?">previous question</a> regarding locking on two List(Of T) objects.  The answer there was helpful but left me with another question.</p>
  
  <p>Suppose I have a function like this:</p>
  
  <pre><code>Public Function ListWork() As Integer
    List1.Clear()
    ..Some other work which does not modify List1..
    List1.AddRange(SomeArray)
    ..Some more work that does not involve List1..
    Return List1.Count
  End Function
  </code></pre>
  
  <p>which resides in a class that declares List1.  In a multithreaded environment, I now understand that I should have a private locking object for List1 and lock List1 whenever it's modified or enumerated.  My question is, should I do this:</p>
  
  <pre><code>Private List1Lock As New Object
  Public Function ListWork() As Integer
    SyncLock List1Lock
      List1.Clear()
    End SyncLock
    ..Some other work which does not modify List1..
    SyncLock List1Lock
      List1.AddRange(SomeArray)
    End SyncLock
    ..Some more work that does not involve List1..
    SyncLock List1Lock
      Dim list1Count As Integer = List1.Count
    End SyncLock
    Return list1Count
  End Function
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>Private List1Lock As New Object
  Public Function ListWork() As Integer
    SyncLock List1Lock
      List1.Clear()
      ..Some other work which does not modify List1..
      List1.AddRange(SomeArray)
      ..Some more work that does not involve List1..
      Dim list1Count As Integer = List1.Count
    End SyncLock
    Return list1Count
  End Function
  </code></pre>
  
  <p>I'm guessing that the former example is optimal? </p>