title: Best thread-safe way to increment an integer up to 65535
body: <p>I have a System.Timers.Timer that increments a counter every 3 seconds. Another thread may also set this variable to any value under some conditions.</p>
  
  <p>Tried to use <strong>Interlocked.Increment</strong> but it does not have an overload for UInt16. The next thing in mind is <strong>lock</strong>, but I am not really sure how to make thread-safe access (read/write/increment) to this variable.</p>
  
  <p>Edited: the code originally used an int, but changed to UInt16 as suggested</p>
  
  <pre><code>private volatile System.UInt16 mCounter = 0;
  private readonly object mCounterLock = new object();
  public System.UInt16 Counter {
    get {
      lock (mCounterLock) {
        return mCounter;
      }
    }
    set {
      lock (mCounterLock) {
        mCounter = value;
      }
    }
  }
  private System.Timers.Timer mCounterTimer;
  
  void mCounter_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    lock (mCounterLock) {
      Counter++;
    }
  }
  </code></pre>