title: How to get rid of CA2000 warning when ownership is transferred?
body: <p>The following code generates two CA2000 warnings (among others, but that's not the point).</p>
  
  <pre><code>public sealed class Item: IDisposable
  {
      public void Dispose() {}
  }
  
  public sealed class ItemContainer
  {
      public void Add(Item item)
      {
      }
  }
  
  public sealed class Test: IDisposable
  {
      private ICollection&lt;Item&gt; itemCollection;
      private ItemContainer itemContainer;
  
      private void Add(Item item)
      {
          itemCollection.Add(item);
      }
  
      public void Initialize()
      {
          var item1 = new Item(); // no warning
          itemCollection.Add(item1);
  
          var item2 = new Item(); // CA2000: call Dispose on object item2
          Add(item2);
  
          var item3 = new Item(); // CA2000: call Dispose on object item3
          itemContainer.Add(item3);
      }
  
      public void Dispose() {}
  }
  </code></pre>
  
  <p>Note that there is no warning generated for item1. It seems, Code Analysis assumes the <code>ICollection</code> will take responsibility of the item and eventually dispose it.</p>
  
  <p>Is there a way to mark my <code>Add</code> methods, so that the warning goes away?</p>
  
  <p>I'm looking for something similar to <code>ValidatedNotNullAttribute</code> for CA1062.</p>
  
  <p>Edit: to make it clear: this is not my real code. In the real code, everything is properly disposed. </p>
  
  <p>It's just that CA does not recognize that the call to my <code>Add</code> methods transfers ownership.
  I would like it to treat my Add methods in the same way it treats <code>ICollection.Add</code>.</p>
  
  <p>Disposing in the same scope is not an option.</p>