title: Why can't we lock on a value type?
body: <p>I was trying to <code>lock</code> a <code>Boolean</code> variable when I encountered the following error :</p>
  
  <blockquote>
    <p>'bool' is not a reference type as required by the lock statement</p>
  </blockquote>
  
  <p>It seems that only reference types are allowed in <code>lock</code> statements, but I'm not sure I understand why.</p>
  
  <p>Andreas is stating in his <a href="https://stackoverflow.com/questions/420825/how-to-properly-lock-a-value-type">comment</a>:</p>
  
  <blockquote>
    <p>When [a value type] object is passed from one thread to the other, a copy is made, so the threads end up working on 2 different objects, which is safe.</p>
  </blockquote>
  
  <p>Is it true? Does that mean that when I do the following, I am in fact modifying two different <code>x</code> in the <code>xToTrue</code> and the <code>xToFalse</code> method?</p>
  
  <pre><code>public static class Program {
  
      public static Boolean x = false;
  
      [STAThread]
      static void Main(string[] args) {
  
          var t = new Thread(() =&gt; xToTrue());
          t.Start();
          // ...
          xToFalse();
      }
  
      private static void xToTrue() {
          Program.x = true;
      }
  
      private static void xToFalse() {
          Program.x = false;
      }
  }
  </code></pre>
  
  <p>(this code alone is clearly useless in its state, it is only for the example)</p>
  
  <hr>
  
  <p>P.S: I know about this question on <a href="https://stackoverflow.com/questions/420825/how-to-properly-lock-a-value-type">How to properly lock a value type</a>. My question is not related to the <em>how</em> but to the <em>why</em>.</p>