title: c# foreach with locked property
body: <p>Alright, so here is the property I have</p>
  
  <pre><code>public List&lt;String&gt; names{
      get{
          lock(_names)
              return _names;
         }
     set{
         lock(_names)
             _names = value
        }
  }
  private List&lt;String&gt; _names;
  </code></pre>
  
  <p>and now say I do a foreach on names like this</p>
  
  <pre><code>foreach(String s in names)
  {
      Console.WriteLine(s);
  }
  </code></pre>
  
  <p>My question is, is <code>names</code> locked through the whole <code>foreach</code>, or does it only <code>lock</code> each time <code>s</code> is set, then unlocks inside the <code>foreach</code>.</p>
  
  <p>If that's confusing, say I try to do this</p>
  
  <pre><code>foreach(String s in names)
  {
      lock(names)
          Console.WriteLine(s);
  }
  </code></pre>
  
  <p>Will I end up in a deadlock?</p>