内部 System.Linq.Set 与公共 System.Collections.Generic.HashSet

2024-01-04

查看这段代码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; 
    }

为什么微软的人决定使用这个内部实现Set而不是常规的HashSet?如果它在任何方面都更好,为什么不将其暴露给公众呢?


本条的实施Set<T>HashSet<T>因为它只需要添加和删除元素并检查 LINQ 内部进程是否存在。它不实现任何接口或公开迭代器等。

因此,对于 LINQ 使用它的目的来说,它可能更快。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

内部 System.Linq.Set 与公共 System.Collections.Generic.HashSet 的相关文章

随机推荐