Created
December 7, 2021 11:31
-
-
Save in-async/593e4ad2ea6894252b70776c72b53d02 to your computer and use it in GitHub Desktop.
IEnumerable<T> の列挙が完了したら指定したオブジェクトを自動的に Dispose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Commons { | |
public static class EnumerableExtensions { | |
public static IEnumerable<T> WithDisposable<T>(this IEnumerable<T> source, IDisposable disposable) { | |
try { | |
foreach (T item in source) { | |
yield return item; | |
} | |
} | |
finally { | |
disposable.Dispose(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment