title: Is it safe to use field inside and outside synchronized block?
body: <p><strong>Background</strong></p>
  
  <p>Our app sends emails which are queued in a database table.  We've had some instances of duplicate emails being sent, so I'm implementing a lock to prevent multiple threads from sending emails simultaneously.</p>
  
  <p>ReSharper is warning me that:</p>
  
  <blockquote>
    <p>the field is sometimes used inside synchronized block and sometimes
    used without synchronization</p>
  </blockquote>
  
  <p><strong>Question</strong></p>
  
  <p>Why is ReSharper telling me this, and why might I be worried about it?</p>
  
  <p><strong>Code</strong></p>
  
  <p>Here's my (abridged) code:</p>
  
  <pre><code>private readonly IMailQueueRepository _mailQueueRepository = new MailQueueRepository();
  private static object _messageQueueLock = new object();
  
  public void SendAllQueuedMessages(IPrincipal caller)
  {
      lock (_messageQueueLock) // Prevent concurrent callers
      {
          var message = _mailQueueRepository.GetUnsentMessage();
          while (message != null)
          {
              SendQueuedMessage(message);
              message = _mailQueueRepository.GetUnsentMessage();
          }
      }
  }
  
  public void SendQueuedMessage(IMessage message)
  {
      // I get the ReSharper warning here on _mailQueueRepository
      var messageAttachments = _mailQueueRepository.GetMessageAttachments(message.Id);
      // etc.
  }
  </code></pre>