forked from idg10/prog-cs-8-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForeachDisposal.cs
38 lines (36 loc) · 896 Bytes
/
ForeachDisposal.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
using System;
using System.Collections.Generic;
using System.IO;
namespace Disposable
{
public static class ForeachDisposal
{
public static void SimpleForeach()
{
foreach (string file in Directory.EnumerateFiles(@"C:\temp"))
{
Console.WriteLine(file);
}
}
public static void HowForeachExpands()
{
IEnumerator<string> e =
Directory.EnumerateFiles(@"C:\temp").GetEnumerator();
try
{
while (e.MoveNext())
{
string file = e.Current;
Console.WriteLine(file);
}
}
finally
{
if (e != null)
{
((IDisposable)e).Dispose();
}
}
}
}
}