-
Notifications
You must be signed in to change notification settings - Fork 56
/
Program.cs
55 lines (48 loc) · 1.29 KB
/
Program.cs
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
using System;
namespace MutableStructPitfalls
{
public struct DisposableValue : IDisposable
{
private bool _disposedYet;
public void Dispose()
{
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
}
else
{
Console.WriteLine("Was already disposed");
}
}
}
class Program
{
static void CallDispose(IDisposable o)
{
o.Dispose();
}
static void Main(string[] args)
{
var dv = new DisposableValue();
Console.WriteLine("Passing value variable:");
CallDispose(dv);
CallDispose(dv);
CallDispose(dv);
IDisposable id = dv;
Console.WriteLine("Passing interface variable:");
CallDispose(id);
CallDispose(id);
CallDispose(id);
Console.WriteLine("Calling Dispose directly on value variable:");
dv.Dispose();
dv.Dispose();
dv.Dispose();
Console.WriteLine("Passing value variable:");
CallDispose(dv);
CallDispose(dv);
CallDispose(dv);
}
}
}