Let's imagine I have a simple class that is disposable:
class CanDispose : IDisposable
{
...
}
Now I can obviously put this in a "using" block to make it dispose:
using (var myDisposable = new CanDispose())
{
...
}
But what if I have a factory method that returns the disposable via an out parameter? In my example there can be multiple returns, so multiple outs are clean:
public bool CreateDisposable(out CanDispose canDispose)
{
canDispose = new CanDispose();
...
return ret;
}
How do I put this in a using statement? Simply putting the function call into a using block doesn't seem to work. Do I have to resort to returning a Tuple (will that work?), or is there a simpler way?
Note, this doesn't appear to work. At least it doesn't shut up the warning about disposing before going out of scope:
using (CreateDisposable(out CanDispose myDispose))
{
....
}
CreateDisposable
method needs to return abool
...