IntersectWith
is a mutable operation where Intersect
is immutable. This means that IntersectWith
modifies duplicateWith
whereas Intersect
will generate a new IEnumerable
containing the duplicates.
If you change line 12 with this then the value that's returned from Intersect
will be set back to the original variable:
duplicate = duplicate.Intersect(Family).ToHashSet();
Note that because Intersect
returns an IEnumerable
rather than a HashSet
the .ToHashSet()
call at the end will convert it.