blob: 34cdd0c2f9473f04ea4e6013a6f6b95ba72b7461 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
//
// Tests the using statement implementation
//
using System;
using System.IO;
class MyDispose : IDisposable {
public bool disposed;
public void Dispose ()
{
disposed = true;
}
}
//
// This class does not implement IDiposable, but has an implicit conversion
// defined
//
class NoIDispose {
static public MyDispose x;
public NoIDispose ()
{
}
static NoIDispose ()
{
x = new MyDispose ();
}
public static implicit operator MyDispose (NoIDispose a)
{
return x;
}
}
class Y {
static void B ()
{
using (NoIDispose a = new NoIDispose ()){
}
}
}
class X {
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;
}
}
|