Call Distinct <> () in a HashSet <T>

I'm just curious. When I call Distinct <> () (Linq) on a HashSet, does .NET know that this IEnumerable always contains a specific set of values ​​and optimizes this call?

+6
source share
3 answers

Judging by viewing the code through Reflector, I should have said no.

As a result of the code, an instance of the class generated by the iterator method is created, regardless of what type you give it.

This problem is also compounded by the fact that you can specify mapping objects for the Hashset and Distinct methods, which means that optimization will only be used in very few cases.

For example, in the following case, he can actually optimize the call, but he will not be able to know that:

 var set = new HashSet<int>(new MyOwnInt32Comparer()); var distinct = set.Distinct(new MyOwnInt32Comparer()); 

Since I give him two instances of the comparison class, and such classes usually do not implement equality methods, the Distinct method would have no idea that the two implementation comparisons are really identical.

In any case, this is the case when the programmer knows more about the code than the runtime, so use it. Linq can be very good, but it is not omnipotent, so use your knowledge to your advantage.

+11
source

I think not, because the input of the Enumerable class is for a separate IEnumerable method and there is nothing concrete to define its hash set (so do nothing).

+2
source

No, looking at the implementation in the reflector, it does not check if the enumeration is HashSet<T> . The main iterator creates a new set and fills it during the enumeration, so the overhead should not be so large.

+2
source

All Articles