public static int UsingTest() { var myDispose = new MyDispose(new [] { 57 }); int num; using (myDispose) num = myDispose.X_field[0]; return(num + myDispose.X_field[0]); // 67 }
static int Main () { MyDispose copy_a, copy_b, copy_c; // // Test whether the two `a' and `b' get disposed // using (MyDispose a = new MyDispose (), b = new MyDispose ()){ copy_a = a; copy_b = b; } if (!copy_a.disposed) return 1; if (!copy_b.disposed) return 2; Console.WriteLine ("Nested using clause disposed"); // // See if the variable `b' is disposed if there is // an error thrown inside the using block. // copy_c = null; try { using (MyDispose c = new MyDispose ()){ copy_c = c; throw new Exception (); } } catch {} if (!copy_c.disposed) return 3; else Console.WriteLine ("Disposal on finally block works"); // // This should test if `a' is non-null before calling dispose // implicitly // using (MyDispose d = null){ } Console.WriteLine ("Null test passed"); MyDispose bb = new MyDispose (); using (bb){ } if (bb.disposed == false) return 6; Console.WriteLine ("All tests pass"); return 0; }
// OUT: // TRUE if the object was DISPOSED // FALSE if the object was NOT DISPOSED public bool ReturnFromUsing(MyDispose m) { using (m) { if (!m.IsDisposed) { return(false); } } return(true); }
// OUT: // TRUE if the object was DISPOSED // FALSE if the object was NOT DISPOSED public bool GotoFromUsing(MyDispose m) { using (m) { if (!m.IsDisposed) { goto EXIT; } } return(true); EXIT: return(false); }
public int GotoTests() { MyDispose m = new MyDispose(); bool wasDisposed; // Called with object not disposed m.DisposeCounter = 0; m.IsDisposed = false; wasDisposed = GotoFromUsing(m); if (1 != m.DisposeCounter) { Console.WriteLine("GotoTests1: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return(-1); } if (wasDisposed) { // the object should not have been disposed entering // this method call // if the object was disposed then there was an issue Console.WriteLine("GotoTests1: Object was Disposed upon entering the method call (in error)"); return(-2); } // called with object disposed m.DisposeCounter = 0; m.IsDisposed = true; wasDisposed = GotoFromUsing(m); if (1 != m.DisposeCounter) { Console.WriteLine("GotoTests2: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return(-1); } if (!wasDisposed) { // the object should have been disposed entering // this method call // if the object was not disposed then there was an issue Console.WriteLine("GotoTests2: Object was not Disposed upon entering the method call (in error)"); return(-2); } return(0); }
// OUT: // TRUE if the object was DISPOSED // FALSE if the object was NOT DISPOSED public bool WorkingSwitch(int msg, MyDispose m) { bool result = true; switch (msg) { case 0: using (m) { if (!m.IsDisposed) { result = false; } } break; default: break; } return result; }
// OUT: // TRUE if the object was DISPOSED // FALSE if the object was NOT DISPOSED public bool WorkingSwitch(int msg, MyDispose m) { bool result = true; switch (msg) { case 0: using (m) { if (!m.IsDisposed) { result = false; } } break; default: break; } return(result); }
public int SwitchTests() { MyDispose m = new MyDispose(); bool wasDisposed; // Called with object not disposed m.DisposeCounter = 0; m.IsDisposed = false; wasDisposed = BrokenSwitch(0, m); if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests1: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return(-1); } m.DisposeCounter = 0; m.IsDisposed = false; wasDisposed = WorkingSwitch(0, m) || wasDisposed; if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests2: MyDispose.Dispose() called too many times 12 != {0}", m.DisposeCounter); return(-1); } if (wasDisposed) { // the object should not have been disposed entering // these method calls // if the object was disposed then there was an issue Console.WriteLine("SwitchTests1: Object was Disposed upon entering the method call (in error)"); return(-2); } // called with object disposed m.DisposeCounter = 0; m.IsDisposed = true; wasDisposed = BrokenSwitch(0, m); if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests3: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return(-1); } m.DisposeCounter = 0; m.IsDisposed = true; wasDisposed = WorkingSwitch(0, m) && wasDisposed; if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests4: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return(-1); } if (!wasDisposed) { // the object should have been disposed entering // these method calls // if the object was not disposed then there was an issue Console.WriteLine("SwitchTests2: Object was not Disposed upon entering the method call (in error)"); return(-2); } return(0); }
public int SwitchTests() { MyDispose m = new MyDispose(); bool wasDisposed; // Called with object not disposed m.DisposeCounter = 0; m.IsDisposed = false; wasDisposed = BrokenSwitch(0, m); if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests1: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return -1; } m.DisposeCounter = 0; m.IsDisposed = false; wasDisposed = WorkingSwitch(0, m) || wasDisposed; if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests2: MyDispose.Dispose() called too many times 12 != {0}", m.DisposeCounter); return -1; } if (wasDisposed) { // the object should not have been disposed entering // these method calls // if the object was disposed then there was an issue Console.WriteLine("SwitchTests1: Object was Disposed upon entering the method call (in error)"); return -2; } // called with object disposed m.DisposeCounter = 0; m.IsDisposed = true; wasDisposed = BrokenSwitch(0, m); if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests3: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return -1; } m.DisposeCounter = 0; m.IsDisposed = true; wasDisposed = WorkingSwitch(0, m) && wasDisposed; if (1 != m.DisposeCounter) { Console.WriteLine("SwitchTests4: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return -1; } if (!wasDisposed) { // the object should have been disposed entering // these method calls // if the object was not disposed then there was an issue Console.WriteLine("SwitchTests2: Object was not Disposed upon entering the method call (in error)"); return -2; } return 0; }
static int Main() { MyDispose copy_a, copy_b, copy_c; // // Test whether the two `a' and `b' get disposed // using (MyDispose a = new MyDispose(), b = new MyDispose()){ copy_a = a; copy_b = b; } if (!copy_a.disposed) { return(1); } if (!copy_b.disposed) { return(2); } Console.WriteLine("Nested using clause disposed"); // // See if the variable `b' is disposed if there is // an error thrown inside the using block. // copy_c = null; try { using (MyDispose c = new MyDispose()){ copy_c = c; throw new Exception(); } } catch {} if (!copy_c.disposed) { return(3); } else { Console.WriteLine("Disposal on finally block works"); } // // This should test if `a' is non-null before calling dispose // implicitly // using (MyDispose d = null){ } Console.WriteLine("Null test passed"); // // This tests that a variable is permitted here if there is // an implicit conversion to a type that implement IDisposable // // This is legal according to ECMA-334 15.13 // "If there is not an implicit conversion from ResourceType // to the System.IDisposable interface, then a compile-time // error is produced." // But .NET only compiles, if we cast the object to MyDispose ourselves using (MyDispose a = new NoIDispose()){ } // // See if we dispose the object that can be implicitly converted // to IDisposable if (NoIDispose.x.disposed != true) { return(4); } else { Console.WriteLine("Implicit conversion from type to IDisposable pass"); } MyDispose bb = new MyDispose(); using (bb){ } if (bb.disposed == false) { return(6); } Console.WriteLine("All tests pass"); return(0); }
static NoIDispose() { x = new MyDispose(); }
static int Main () { MyDispose copy_a, copy_b, copy_c; // // Test whether the two `a' and `b' get disposed // using (MyDispose a = new MyDispose (), b = new MyDispose ()){ copy_a = a; copy_b = b; } if (!copy_a.disposed) return 1; if (!copy_b.disposed) return 2; Console.WriteLine ("Nested using clause disposed"); // // See if the variable `b' is disposed if there is // an error thrown inside the using block. // copy_c = null; try { using (MyDispose c = new MyDispose ()){ copy_c = c; throw new Exception (); } } catch {} if (!copy_c.disposed) return 3; else Console.WriteLine ("Disposal on finally block works"); // // This should test if `a' is non-null before calling dispose // implicitly // using (MyDispose d = null){ } Console.WriteLine ("Null test passed"); // // This tests that a variable is permitted here if there is // an implicit conversion to a type that implement IDisposable // // This is legal according to ECMA-334 15.13 // "If there is not an implicit conversion from ResourceType // to the System.IDisposable interface, then a compile-time // error is produced." // But .NET only compiles, if we cast the object to MyDispose ourselves using (MyDispose a = new NoIDispose ()){ } // // See if we dispose the object that can be implicitly converted // to IDisposable if (NoIDispose.x.disposed != true) return 4; else Console.WriteLine ("Implicit conversion from type to IDisposable pass"); MyDispose bb = new MyDispose (); using (bb){ } if (bb.disposed == false) return 6; Console.WriteLine ("All tests pass"); return 0; }
public static int Main() { MyDispose copy_a, copy_b, copy_c; // // Test whether the two `a' and `b' get disposed // using (MyDispose a = new MyDispose(), b = new MyDispose()){ copy_a = a; copy_b = b; } if (!copy_a.disposed) { return(1); } if (!copy_b.disposed) { return(2); } Console.WriteLine("Nested using clause disposed"); // // See if the variable `b' is disposed if there is // an error thrown inside the using block. // copy_c = null; try { using (MyDispose c = new MyDispose()){ copy_c = c; throw new Exception(); } } catch {} if (!copy_c.disposed) { return(3); } else { Console.WriteLine("Disposal on finally block works"); } // // This should test if `a' is non-null before calling dispose // implicitly // using (MyDispose d = null){ } Console.WriteLine("Null test passed"); MyDispose bb = new MyDispose(); using (bb){ } if (bb.disposed == false) { return(6); } Console.WriteLine("All tests pass"); return(0); }
// OUT: // TRUE if the object was DISPOSED // FALSE if the object was NOT DISPOSED public bool ReturnFromUsing(MyDispose m) { using (m) { if (!m.IsDisposed) { return false; } } return true; }
public int GotoTests() { MyDispose m = new MyDispose(); bool wasDisposed; // Called with object not disposed m.DisposeCounter = 0; m.IsDisposed = false; wasDisposed = GotoFromUsing(m); if (1 != m.DisposeCounter) { Console.WriteLine("GotoTests1: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return -1; } if (wasDisposed) { // the object should not have been disposed entering // this method call // if the object was disposed then there was an issue Console.WriteLine("GotoTests1: Object was Disposed upon entering the method call (in error)"); return -2; } // called with object disposed m.DisposeCounter = 0; m.IsDisposed = true; wasDisposed = GotoFromUsing(m); if (1 != m.DisposeCounter) { Console.WriteLine("GotoTests2: MyDispose.Dispose() called too many times 1 != {0}", m.DisposeCounter); return -1; } if (!wasDisposed) { // the object should have been disposed entering // this method call // if the object was not disposed then there was an issue Console.WriteLine("GotoTests2: Object was not Disposed upon entering the method call (in error)"); return -2; } return 0; }
static NoIDispose () { x = new MyDispose (); }
static int Main () { MyDispose copy_a, copy_b, copy_c; // // Test whether the two `a' and `b' get disposed // using (MyDispose a = new MyDispose (), b = new MyDispose ()){ copy_a = a; copy_b = b; } if (!copy_a.disposed) return 1; if (!copy_b.disposed) return 2; Console.WriteLine ("Nested using clause disposed"); // // See if the variable `b' is disposed if there is // an error thrown inside the using block. // copy_c = null; try { using (MyDispose c = new MyDispose ()){ copy_c = c; throw new Exception (); } } catch {} if (!copy_c.disposed) return 3; else Console.WriteLine ("Disposal on finally block works"); // // This should test if `a' is non-null before calling dispose // implicitly // using (MyDispose d = null){ } Console.WriteLine ("Null test passed"); // // This tests that a variable is permitted here if there is // an implicit conversion to a type that implement IDisposable // using (NoIDispose a = new NoIDispose ()){ } // // See if we dispose the object that can be implicitly converted // to IDisposable if (NoIDispose.x.disposed != true) return 4; else Console.WriteLine ("Implicit conversion from type to IDisposable pass"); MyDispose bb = new MyDispose (); using (bb){ } if (bb.disposed == false) return 6; Console.WriteLine ("All tests pass"); return 0; }
// OUT: // TRUE if the object was DISPOSED // FALSE if the object was NOT DISPOSED public bool GotoFromUsing(MyDispose m) { using (m) { if (!m.IsDisposed) { goto EXIT; } } return true; EXIT: return false; }