[英]Internal System.Linq.Set<T> vs public System.Collections.Generic.HashSet<T>
Check out this piece of code from Linq.Enumerable
class: 查看
Linq.Enumerable
类中的这段代码:
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
? 为什么Microsoft的人决定使用
Set
内部实现而不是常规的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. 这个
Set<T>
的实现比HashSet<T>
简单得多,因为它只需要添加和删除元素并检查LINQ内部进程是否存在。 It does not implement any interfaces or expose iterators etc. 它没有实现任何接口或暴露迭代器等。
So probably it is faster for the purpose LINQ uses it for. 所以LINQ使用它的目的可能更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.