-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
25 lines (20 loc) · 945 Bytes
/
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
// Reference: https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/november/net-framework-hidden-disposables
using LearnPotentialDisposable;
ItemFactory factory = new ItemFactory();
// Method 1: Check the IDisposable manually
ISomething createdItem = factory.CreateSomething();
Console.WriteLine("#1 Create {0}, is IDisposable: {1}", createdItem, createdItem is IDisposable);
if (createdItem is IDisposable disposable)
{
disposable.Dispose();
}
// Method 2: Use an extension method
ISomething createdItem2 = factory.CreateSomething();
Console.WriteLine("#2 Create {0}, is IDisposable: {1}", createdItem2, createdItem2 is IDisposable);
createdItem2.TryDispose();
// Method 3: Use potential disposable wrapper
ISomething createdItem3 = factory.CreateSomething();
using (new PotentialDisposable<ISomething>(createdItem3))
{
Console.WriteLine("#3 Create {0}, is IDisposable: {1}", createdItem3, createdItem3 is IDisposable);
}