Merge pull request #4935 from lambdageek/dev-handles-may
[mono.git] / mcs / tests / test-53.cs
1 //
2 // Tests the using statement implementation
3 //
4 using System;
5 using System.IO;
6
7 class MyDispose : IDisposable {
8         public bool disposed;
9         
10         public void Dispose ()
11         {
12                 disposed = true;
13         }
14 }
15
16 class X {
17         public static int Main ()
18         {
19                 MyDispose copy_a, copy_b, copy_c;
20
21                 //
22                 // Test whether the two `a' and `b' get disposed
23                 //
24                 using (MyDispose a = new MyDispose (), b = new MyDispose ()){
25                         copy_a = a;
26                         copy_b = b;
27                 }
28
29                 if (!copy_a.disposed)
30                         return 1;
31                 if (!copy_b.disposed)
32                         return 2;
33
34                 Console.WriteLine ("Nested using clause disposed");
35
36                 //
37                 // See if the variable `b' is disposed if there is
38                 // an error thrown inside the using block.
39                 //
40                 copy_c = null;
41                 try {
42                         using (MyDispose c = new MyDispose ()){
43                                 copy_c = c;
44                                 throw new Exception ();
45                         }
46                 } catch {}
47
48                 if (!copy_c.disposed)
49                         return 3;
50                 else
51                         Console.WriteLine ("Disposal on finally block works");
52
53                 //
54                 // This should test if `a' is non-null before calling dispose
55                 // implicitly
56                 //
57                 using (MyDispose d = null){
58                 }
59
60                 Console.WriteLine ("Null test passed");
61                 
62                 MyDispose bb = new MyDispose ();
63                 using (bb){
64                         
65                 }
66                 if (bb.disposed == false)
67                         return 6;
68                 
69                 Console.WriteLine ("All tests pass");
70                 return 0;
71         }
72 }
73