Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal System.Linq.Set<T> vs public System.Collections.Generic.HashSet<T>

Tags:

c#

linq

hashset

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?

like image 925
HuBeZa Avatar asked Jun 12 '13 08:06

HuBeZa


1 Answers

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.

like image 186
okrumnow Avatar answered Nov 15 '22 13:11

okrumnow