title: Does C# automatically "dispose" IDisposable objects when they fall out of scope?
body: <p>I am writing an ASP.NET Web API. As part of this, I have a class that I found out, during testing, needs to implement <code>IDisposable</code> to ensure managed resources are freed. </p>
  
  <p>So, I implemented <code>IDisposable</code> in my class, and put the code necessary to free the resources in the <code>Dispose()</code> method.</p>
  
  <p>There are many places in my code (hundreds) where I instantiate this object, and in the same line call a method on the new instance. I only need the instance to call the single method. </p>
  
  <p>Here's an example:</p>
  
  <pre><code>// MyObject, a class that needs to be disposed of.
  public class MyObject : IDisposable
  {
      private AnObjectThatMustBeDisposed localObject;
  
      public MyObject() 
      {
          localObject = SomeLibrary.SomeProject.AnObjectThatMustBeDisposed.Create();
      }
  
      public void doOperationOne()
      {
          localObject.DoSomething(1);
      }
  
      public string getOperationTwo()
      {
          return localObject.DoSomething(2);
      }
  
      public string getOperationThree()
      {
          return localObject.DoSomething(3);
      }
  
      public bool getOperationFour(string input)
      {
          return localObject.DoSomethingSpecial(4,input.ToLower());
      }
  
      ...
  
      public void getOperationOneHundred(DateTime input)
      {
          localObject.DoSomethingElse(100,input);
      }
  
      public void Dispose()
      {
          localObject.CloseResources();
          localObject.FreeUpMemory();
          localObject.Close();
          localObject.Dispose();
      }
  }
  
  // A class that makes use of MyObject
  public class MyLibraryThatUsesMyObject
  {
      public void Method1()
      {
          new MyObject().doOperationOne();
      }
      public string Method2()
      {
          return new MyObject().getOperationTwo();
      }
      public int Method3()
      {
          return new MyObject().getOperationThree();
      }
      public bool Method4(string testString)
      {
          if (testString.Length &gt; 6)
          {
              if (new MyObject().getOperationFour(testString)) return true;
              else return false;
          }
          else return false;
      }
  
      ...
  
      public void Method100()
      {
          new MyObject().doOperationOneHundred(DateTime.Now);
      }
  }
  </code></pre>
  
  <p>My question is: Does .NET automatically <code>Dispose()</code> objects when they fall out of scope? Or, do I actually have to do this... </p>
  
  <pre><code>public void Method1() 
  {
      using (MyObject o = new MyObject())
      {
          o.DoOperationOne();
      }
  }
  </code></pre>
  
  <p>...to each method? It wouldn't be hard if I had two or three methods, but if I have tons of methods, this refactoring could take quite a while.</p>
  
  <p>I am not sure how ASP.NET handles requests as they complete - i.e. does the framework give code time to <code>Dispose()</code> things, or does it "cut off" execution as soon as the <code>return</code> is called, not letting things dispose?</p>
  
  <p>The fact that, without implementing <code>IDisposable</code> myself, things inside the <code>MyObject</code> class are failing due to unreleased resources causing leaks, it feels like .NET does <em>not</em> automatically <code>Dispose</code> things. So, if that's the case, can I do something so I don't have to refactor hundreds of methods?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: I tried simply implementing <code>IDisposable</code>, but my unit test was still able to produce a resource leak. So it would appear that my suspicion that .NET is not automatically disposing is correct. So now my question becomes - how can I force disposing without having to refactor hundreds of methods?</p>