title: Who should call Dispose on IDisposable objects when passed into another object?
body: <p>Is there any guidance or best practices around who should call <code>Dispose()</code> on disposable objects when they have been passed into another object's methods or constuctor?</p>
  
  <p>Here's a couple of examples as to what I mean.</p>
  
  <p>IDisposable object is passed into a method (Should it dispose of it once its done?):</p>
  
  <pre><code>public void DoStuff(IDisposable disposableObj)
  {
      // Do something with disposableObj
      CalculateSomething(disposableObj)
  
      disposableObj.Dispose();
  }
  </code></pre>
  
  <p>IDisposable object is passed into a method and a reference is kept (Should it dispose of it when <code>MyClass</code> is disposed?):</p>
  
  <pre><code>public class MyClass : IDisposable
  {
      private IDisposable _disposableObj = null;
  
      public void DoStuff(IDisposable disposableObj)
      {
          _disposableObj = disposableObj;
      }
  
      public void Dispose()
      {
          _disposableObj.Dispose();
      }
  }
  </code></pre>
  
  <p>I'm currently thinking that in the first example the <em>caller</em> of <code>DoStuff()</code> should dispose of the object as it probably created the object. But in the second example it feels like <code>MyClass</code> should dispose of the object as it keeps a reference to it. The problem with this is that the calling class might not know <code>MyClass</code> has kept a reference and therefore might decide to dispose of the object before <code>MyClass</code> has finished using it.
  Are there any standard rules for this sort of scenario? If there are, do they differ when the disposable object is being passed into a constructor?</p>