Say I have a method like this (stolen from a previous SO answer by Jon Skeet):
public static IEnumerable<TSource> DuplicatesBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
// Yield it if the key hasn't actually been added - i.e. it
// was already in the set
if (!seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
In this method I have a HashSet that is used to hold keys that have been seen. If I use this method in something like this.
List<string> strings = new List<string> { "1", "1", "2", "3" };
List<string> somewhatUniques = strings.DuplicatesBy(s => s).Take(2);
This will only enumerate over the first 2 items in the strings list. But how does garbage collection collect the seenKeys hashset. Since yield just pauses the execution of the method, if the method is expensive how can I make sure I dispose of things properly?