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.
source share