im wondering if we can join using statement and deconstruct operation. To be more visual look at below sample:
using System;
public class Foo : IDisposable
{
public IDisposable Bar { get; set; }
public IDisposable Baz { get; set; }
public void Deconstruct(out IDisposable bar, out IDisposable baz)
{
bar = Bar;
baz = Baz;
}
public void Dispose()
{
Bar.Dispose();
Baz.Dispose();
}
}
public class Program
{
public static void Main()
{
// using var foo = new Foo(); // this is ok
// var (bar, baz) = new Foo(); // this is also ok, but i need to dispose each variable
using var (bar, baz) = new Foo(); // this gives error
}
}
I'm wrongly assuming that statement using var (bar, baz) = new Foo(); should work? My theory is that both deconstructed variables implements IDisposable, even class Foo itself implements it, but all im getting is IDE errors.
Do you have any ideas why this is not working? How can i dispose all deconstructed values in single call?