内部系统.Linq.Set< T>与公共System.Collections.Generic.HashSet< T> [英] Internal System.Linq.Set<T> vs public System.Collections.Generic.HashSet<T>

查看:67
本文介绍了内部系统.Linq.Set< T>与公共System.Collections.Generic.HashSet< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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; 
    }

为什么微软的人决定使用Set的这种内部实现而不是常规的HashSet?如果以任何方式更好,为什么不将其公开?

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?

推荐答案

Set<T>的实现比HashSet<T>简单得多,因为它只需要添加和删除元素并检查LINQ内部过程是否存在.它不实现任何接口或公开迭代器等.

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.

因此,对于LINQ而言,它可能更快.

So probably it is faster for the purpose LINQ uses it for.

这篇关于内部系统.Linq.Set&lt; T&gt;与公共System.Collections.Generic.HashSet&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆