The Code Analysis-friendly way to dispose of objects
The problem that is being detected by CA2000 in this case is that a disposable instance may be "orphaned" if an exception occurs before it is passed out of the method. For example, a "correct" implementation of CreateFirstObject would look something like the following:
private SomeObject CreateFirstObject()
{
var theObject = new SomeObject();
try
{
theObject.FirstProperty = "some value";
}
catch
{
theObject.Dispose();
throw;
}
return theObject;
}
Given what you have described concerning your desired behaviour for MainMethod, its "correct" implementation might look something like this:
public void MainMethod()
{
var object1 = CreateFirstObject();
try
{
SomeCollectionProperty.Add(object1);
var object2 = CreateSecondObject();
try
{
SomeCollectionProperty.Add(object2);
}
catch
{
object2.Dispose();
throw;
}
}
catch
{
object1.Dispose();
SomeCollectionProperty.Remove(object1); // Not supposed to throw if item does not exist in collection.
throw;
}
}