using System;
using System.Collections;
using System.Collections.Generic;
public class Test
{
public static void Main(string[] args)
{
foreach (var i in new DangerousEnumerable())
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
public class DangerousEnumerable : IEnumerable<int>, IDisposable
{
public IEnumerator<int> GetEnumerator()
{
yield return 1;
yield return 2;
Console.WriteLine("Finished!");
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Dispose()
{
Console.WriteLine("Disposed!");
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnM7CnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwoKcHVibGljIGNsYXNzIFRlc3QKewoJcHVibGljIHN0YXRpYyB2b2lkIE1haW4oc3RyaW5nW10gYXJncykKCXsKCQlmb3JlYWNoICh2YXIgaSBpbiBuZXcgRGFuZ2Vyb3VzRW51bWVyYWJsZSgpKQoJCXsKCQkJQ29uc29sZS5Xcml0ZUxpbmUoaSk7CgkJfQoJCUNvbnNvbGUuUmVhZEtleSgpOwoJfQp9CgpwdWJsaWMgY2xhc3MgRGFuZ2Vyb3VzRW51bWVyYWJsZSA6IElFbnVtZXJhYmxlPGludD4sIElEaXNwb3NhYmxlCnsKCXB1YmxpYyBJRW51bWVyYXRvcjxpbnQ+IEdldEVudW1lcmF0b3IoKQoJewoJCXlpZWxkIHJldHVybiAxOwoJCXlpZWxkIHJldHVybiAyOwoJCUNvbnNvbGUuV3JpdGVMaW5lKCJGaW5pc2hlZCEiKTsKCX0KCglJRW51bWVyYXRvciBJRW51bWVyYWJsZS5HZXRFbnVtZXJhdG9yKCkKCXsKCQlyZXR1cm4gR2V0RW51bWVyYXRvcigpOwoJfQoKCXB1YmxpYyB2b2lkIERpc3Bvc2UoKQoJewoJCUNvbnNvbGUuV3JpdGVMaW5lKCJEaXNwb3NlZCEiKTsKCX0KfQ==