title: Condition false, but code inside the if statement executed
body: <p>I have the following method:</p>
  
  <pre><code>public bool ConnectAsync()
  {
      if (IsConnected)
          throw new InvalidOperationException("Socket is already connected");
  
      if (IsConnecting)
      {
          throw new InvalidOperationException("Attempt to connect in progress");
      }
  
      . . .
  }
  </code></pre>
  
  <p>Where:</p>
  
  <pre><code>    private readonly object padLock = new object();
  
      private bool isConnecting = false;
  
      public bool IsConnected
      {
          get
          {
              lock (padLock)
              { return socket.Connected; }
          }
      }
  
      public bool IsConnecting
      {
          get
          {
              lock (padLock)
              { return isConnecting; }
          }
  
          private set
          {
              lock (padLock)
              { isConnecting = value; }
          }
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/6GYkp.png" alt="enter image description here"></p>
  
  <p>Why the code inside the if statement is executed if my variable isConnecting is false?</p>
  
  <p><strong>Edit</strong>:<br>
  If I use the filed <code>isConnecting</code> instead of the property <code>IsConnecting</code> I have the same behavior. The code runs in the same thread anywhere.</p>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>Finally this works:</p>
  
  <pre><code>lock (padLock)
  {
      if (IsConnecting)
          throw new InvalidOperationException("Attempt to connect in progress");
  }
  </code></pre>
  
  <p>And this works:</p>
  
  <pre><code>{
      if (IsConnecting)
          throw new InvalidOperationException("Attempt to connect in progress");
  }
  </code></pre>
  
  <p><strong>But why?</strong></p>