title: How to dispose of an object that it created in a task?
body: <p>I have a foreach loop that creates multiple tasks like this:</p>
  
  <p>[edit: CreateDisposableAsync returns a Task[IDisposable]]</p>
  
  <pre><code>foreach(...)
  {
     tasks.Add(CreateDisposableAsync());
  }
  </code></pre>
  
  <p>and later I await on all of these tasks, and catch any exceptions:</p>
  
  <pre><code>try
  {
     await Task.WhenAll(tasks);
  }
  catch (AggregateException)
  {
     // handle exceptions
  }
  </code></pre>
  
  <p>but the call to CreateDisposableAsync() returns an IDisposable, which I want to be disposed whether or not there was an exception in any of the tasks. How can I do this?</p>
  
  <p>[Edit: It turns out that the CreateDisposableAsync() function was disposing of its created object if itself threw an exception, so there was nothing wrong with the original code.]</p>