title: Synchronized methods in C#
body: <p>Part of porting a Java application to C# is to implement a synchronized message buffer in C#. By synchronized I mean that it should be safe for threads to write and read messages to and from it.</p>
  
  <p>In Java this can be solved using <code>synchronized</code> methods and <code>wait()</code> and <code>notifyAll()</code>.</p>
  
  <p>Example:</p>
  
  <pre><code>public class MessageBuffer {
      // Shared resources up here
  
      public MessageBuffer() {
          // Initiating the shared resources
      }
  
      public synchronized void post(Object obj) {
          // Do stuff
          wait();
          // Do more stuff
          notifyAll();
          // Do even more stuff
      }
  
      public synchronized Object fetch() {
          // Do stuff
          wait();
          // Do more stuff
          notifyAll();
          // Do even more stuff and return the object
      }
  }
  </code></pre>
  
  <p>How can I achieve something similar in C#?</p>