A simple rule: if you create * it, it is your responsibility to ensure that it is deleted. Conversely, if a method (whether it is a constructor or otherwise) does not create a one-time object, I would not expect it to destroy it. (* Creation includes calls to methods that return IDisposable.)
So something like
public Foo(SomeDisposable obj) : base(obj) { }
I expect to see something like
using (SomeDisposable obj = new SomeDisposable()) { Foo foo = new Foo(obj); }
And not
Foo foo = new Foo(new SomeDisposable());
If you have a constructor without parameters for Foo , and you want to call the base constructor with a one-time object, you are in a much more complex position and probably where the rule is trying to protect you, I would say to avoid such a position, or encoding the base to create, and therefore be responsible for IDisposable, or have a 1: 1 mapping between constuctors and IDisposable parameters, so parameterless Foo does not call a parameterized base.
But there should not be a task of a method or constructor to guess that you ended up with the object in which you passed, because how should it know? You may have other options to use.
source share