title: How to fix CA Dispose objects before losing scope?
body: <p>CA throw the following message:</p>
  
  <pre><code>CA2000 Dispose objects before losing scope.
  In method 'GetMatchingJobsDeferred(BackgroundJobSearchParameters)',
  object '&lt;&gt;g__initLocal0' is not disposed along all exception paths.
  Call System.IDisposable.Dispose on object '&lt;&gt;g__initLocal0' before 
  all references to it are out of scope.
  </code></pre>
  
  <p>I have disposable type <code>CompositeCollection</code>.</p>
  
  <pre><code>public sealed class CompositeCollection&lt;T&gt; : IEnumerable&lt;T&gt;, IDisposable
  {
      private readonly List&lt;IEnumerable&lt;T&gt;&gt; _enumerables = new List&lt;IEnumerable&lt;T&gt;&gt;();
      private readonly List&lt;IDisposable&gt; _disposables = new List&lt;IDisposable&gt;();
  
      public void Add(IEnumerable&lt;T&gt; enumerable)
      {
          _enumerables.Add(enumerable);
      }
  
      public void Add(IDeferredResultCollection&lt;T&gt; deferredResultCollection)
      {
          _enumerables.Add(deferredResultCollection);
          _disposables.Add(deferredResultCollection);
      }
  
      public IEnumerator&lt;T&gt; GetEnumerator()
      {
          return _enumerables.Aggregate((results, enumerable) =&gt; results.Concat(enumerable)).GetEnumerator();
      }
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  
      public void Dispose()
      {
          foreach (var item in _disposables)
          {
              item.Dispose();
          }
      }
  }
  </code></pre>
  
  <p>And that how I used it:</p>
  
  <pre><code>public IDeferredResultCollection&lt;BackgroundJobInfoLight&gt; GetMatchingJobsDeferred(BackgroundJobSearchParameters jobSearchParameters)
  {
      var memoryLocatedJobs = GetInMemoryJobs(jobSearchParameters);
      var databaseLocatedJobs = GetInDatabaseJobsDeferred(jobSearchParameters, memoryLocatedJobs);
  
      return new CompositeCollection&lt;BackgroundJobInfoLight&gt;
      {
          memoryLocatedJobs,
          databaseLocatedJobs
      };
  }
  </code></pre>
  
  <p>I have exception when <code>return</code> statement called.
  How to fix that?</p>
  
  <p>I add <code>try catch</code> and it doesn't help</p>
  
  <pre><code> public IDeferredResultCollection&lt;BackgroundJobInfoLight&gt; GetMatchingJobsDeferred(BackgroundJobSearchParameters jobSearchParameters)
      {
          CompositeCollection&lt;BackgroundJobInfoLight&gt; deferredJobs = null;
          DeferredResultCollection&lt;BackgroundJobInfoLight&gt; databaseLocatedJobs = null;
          try
          {
              var memoryLocatedJobs = GetInMemoryJobs(jobSearchParameters);
              databaseLocatedJobs = GetInDatabaseJobsDeferred(jobSearchParameters, memoryLocatedJobs);
  
              deferredJobs = new CompositeCollection&lt;BackgroundJobInfoLight&gt; { memoryLocatedJobs, databaseLocatedJobs };
              return deferredJobs;
          }
          catch
          {
              if (databaseLocatedJobs != null)
              {
                  databaseLocatedJobs.Dispose();
              }
  
              if (deferredJobs != null)
              {
                  deferredJobs.Dispose();
              }
  
              throw;
          }
      }
  </code></pre>