Check out this piece of code from Linq.Enumerable
class:
static IEnumerable<TSource> DistinctIterator<TSource>(IEnumerable<TSource> source, IEqualityComparer<TSource> comparer) {
Set<TSource> set = new Set<TSource>(comparer);
foreach (TSource element in source)
if (set.Add(element)) yield return element;
}
Why did the guys at Microsoft decided to use this internal implementation of Set
and not the regular HashSet
? If it's better in any way, why not exposing it to the public?
The implementation of this Set<T>
is far simpler than HashSet<T>
as it is only needs to add and remove elements and check for existence for LINQ internal processes. It does not implement any interfaces or expose iterators etc.
So probably it is faster for the purpose LINQ uses it for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With