title: How to automatically dispose objects inside List?
body: <p>I have a class with resources which I need to dispose:</p>
  
  <pre><code>class Desert: IDisposable
  {
      private object resource; // handle to a resource
  
      public Desert(string n)
      {
          // Create resource here
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  
      protected virtual void Dispose(bool disposing)
      {
          if (disposing)
          {
              if (resource != null) resource.Dispose();
          }
      }
  }
  </code></pre>
  
  <p>I wonder if there is any way to automatically ask framework to call Dispose on each element, whenever the List object is going to be destroyed, just like I would have destructor. Currently I am looping through the list and calling Dispose:</p>
  
  <pre><code>// On form open
  List&lt;Desert&gt; desertList = new List&lt;Desert&gt;();
  for(int i = 0; i &lt; 10; i++)
  {
      desertList.Add(new Desert("Desert" + i));
  }
  
  
  // On form closing
  for (int i = 0; i &lt; 10; i++)
  {
      desertList[i].Dispose();
  }
  </code></pre>
  
  <p>Is it the only way to dispose objects inside List?</p>