title: I don't quite understand the workings of using/Disposable objects
body: <p>I asked a question regarding <a href="https://stackoverflow.com/questions/4220039/return-a-disposable-object-for-use-in-a-using-block">returning a Disposable (<code>IDisposable</code>) object from a function</a>, but I thought that I would muddle the discussion if I raised this question there.</p>
  
  <p>I created some sample code:</p>
  
  <pre><code>class UsingTest
  {
      public class Disposable : IDisposable
      {
          public void Dispose()
          {
              var i = 0;
              i++;
          }
      }
      public static Disposable GetDisposable(bool error)
      {
          var obj = new Disposable();
          if (error)
              throw new Exception("Error!");
          return obj;
      }
  }
  </code></pre>
  
  <p>I coded it this way deliberately, because then I call:</p>
  
  <pre><code>using (var tmp = UsingTest.GetDisposable(true)) { }
  </code></pre>
  
  <p>Using the debugger, I notice that the <code>Dispose</code> method never executes, even though we've already instantiated a <code>Disposable</code> object.  If I correctly understand the purpose of <code>Dispose</code>, if this class actually had opened handles and the like, then we would not close them as soon as we had finished with them.</p>
  
  <p>I ask this question because this behavior aligns with what I would expect, but in the answers to the related question, people seemed to indicate that <code>using</code> would take care of everything.</p>
  
  <p>If <code>using</code> still somehow takes care of all of this, could someone explain what I'm missing?  But, if this code could indeed cause resource leak, how would you suggest I code <code>GetDisposable</code> (with the condition that I must instantiate the <code>IDisposable</code> object and run code which could throw an exception prior to the return statement)?</p>