static void Main(string[] args) { var c = new DisposableC(); var cc = c; c.Dispose(); Debug.WriteLine($"c {(c != null ? "!" : "=")}= null"); Debug.WriteLine($"cc {(cc != null ? "!" : "=")}= null"); Foo(1); Foo(2); using (var disposable = new DisposableB()) { // Упс! Метод Dispose не будет вызван ни для // DisposableB, ни для DisposableA } try { using (var disposable = new DisposableB()) { } } catch (Exception eException) { ; } }
static int Foo(int param = 0) { using (var disposable = new DisposableC()) { if (param % 2 == 0) { return(0); } } return(1); }