title: Lock on a variable in multiple threads
body: <p>I am very new to C# and I wanted to ask if I have this situation in MULTI THREADS (pseudo code):</p>
  
  <pre><code>public class ClassA
  {
       ClassB c = new ClassB();
       public void someMethod()
       {
            c.myVar = 1;
            // Some other stuff
            c.myVar = 0;
       }
  }
  
  public class ClassB
  {
       internal int myVar;
  
       public void MethodA()
       {
          if(myVar = 1)
                myVar = 0;
       }
  }
  </code></pre>
  
  <p>If <code>someMethod()</code> and <code>MethodA()</code> can be active in separate threads, then <code>MethodA()</code> could evaluate the if statement as true; but before it sets <code>myVar = 0</code>, <code>someMethod()</code> sets <code>myVar = 0</code> making it incorrect to have set <code>myVar</code> to 0 in <code>MethodA()</code>!!</p>
  
  <p>Basically, how do I lock <code>myVar</code>: </p>
  
  <ul>
  <li>can I <code>lock{}</code> on <code>myVar</code>'s property (set, get)  </li>
  <li>do I need to use <code>Interlock</code> (I have no experience yet of <code>Interlock</code> though)?</li>
  </ul>