Skip to content

Instantly share code, notes, and snippets.

@in-async
Created December 7, 2021 11:31
Show Gist options
  • Save in-async/593e4ad2ea6894252b70776c72b53d02 to your computer and use it in GitHub Desktop.
Save in-async/593e4ad2ea6894252b70776c72b53d02 to your computer and use it in GitHub Desktop.
IEnumerable<T> の列挙が完了したら指定したオブジェクトを自動的に Dispose
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