forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingDeclarationsRefStruct.cs
More file actions
62 lines (56 loc) · 1.86 KB
/
UsingDeclarationsRefStruct.cs
File metadata and controls
62 lines (56 loc) · 1.86 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ExploreCsharpEight
{
internal class ResourceHog : IDisposable
{
private string name;
private bool beenDisposed;
public ResourceHog(string name) => this.name = name;
public void Dispose()
{
beenDisposed = true;
Console.WriteLine($"Disposing {name}");
}
internal void CopyFrom(ResourceHog src)
{
switch (beenDisposed, src.beenDisposed)
{
case (true, true): throw new ObjectDisposedException($"Resource {name} has already been disposed");
case (true, false): throw new ObjectDisposedException($"Resource {name} has already been disposed");
case (false, true): throw new ObjectDisposedException($"Resource {name} has already been disposed");
default: Console.WriteLine($"Copying from {src.name} to {name}"); return;
};
}
}
internal class UsingDeclarationsRefStruct
{
internal int OldStyle()
{
#region Using_Block
using (var src = new ResourceHog("source"))
{
using (var dest = new ResourceHog("destination"))
{
dest.CopyFrom(src);
}
Console.WriteLine("After closing destination block");
}
Console.WriteLine("After closing source block");
#endregion
return 0;
}
internal int NewStyle()
{
#region Using_Declaration
using var src = new ResourceHog("source");
using var dest = new ResourceHog("destination");
dest.CopyFrom(src);
Console.WriteLine("Exiting block");
#endregion
return 0;
}
}
}