Let's say I have a method foo which returns a ValueTuple where one of it's members is disposable, for example (IDisposable, int).
What is the best way to make sure the returned disposable object is correctly disposed on the calling side?
I tried the following:
using (var (disposable, number) = foo())
{
// do some stuff using disposable and number
}
But this won't compile:
'(IDisposable disposable, int number)': type used in a using statement must be implicitly convertible to 'System.IDisposable'
Do I really need to wrap my code in a try-finally block and dispose my object explicitly in the finally block, or is there a nicer way to do it?
Actually I'd like to see the new ValueTuples implement the IDisposable interface and call Dispose() on all their disposable members. Could this be a worthwhile feature request for Microsoft?