2

For me it seems any operation like

var list = new List<int>();
// list.Add some elements...
list.Except(anotherList).Intersect(yetAnotherList)

is this always the same like:

list.Intersect(yetAnotherList).Except(anotherList)

I am not 100% sure.

Thanks for your help.

Daniel Bişar
  • 2,663
  • 7
  • 32
  • 54

1 Answers1

1

From a purely set-theoretic perspective the order does not matter here. In both steps you're just removing elements from list. An element is removed from the list if it's either in anotherList or not in yetAnotherList. Whether you switch the operands of the »or« in the previous sentence doesn't make a difference.

To illustrate, let's construct three lists (sets here, because those are set operations):

list = { A, B, C, D }
anotherList = { A, B, E, F }
yetAnotherList = { A, C, E, G }

Each set here has one element that is in all three sets (A), one element that is in each of the other sets, and one element that is only in that set but not in the others. So we have all possible cases for intersection and set difference covered here.

list.Except(anotherList) yields { C, D }. Intersecting that with yetAnotherList yields { C }.

list.Intersect(yetAnotherList) yields { A, C }. Excepting anotherList yields { C } again.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    It should probably be also noted that both `Intesect` and `Except` preserve the original order of elements - hence both versions yield the same result. – decPL Nov 04 '14 at 15:09
  • Your 2nd sentence is incorrect....kind of a weird way to phrase it. I think you mean you get a list with all elements in yetAnotherList but none of the elements in anotherList. – Kaz Nov 04 '14 at 15:11
  • 1
    @Joey "without all elements in yetAnotherList and without all elements that are not in yetAnotherList" will yield no results. – Kaz Nov 04 '14 at 15:20
  • @Joey - it might have been hard to understand as you've used `yetAnotherList` twice, instead of both `anotherList` and `yetAnotherList`. Meh. – decPL Nov 04 '14 at 15:25
  • 1
    decPL: Ah, now it makes sense. I took the comment as a complaint about my wording with the double-negative instead of an actual factual error. – Joey Nov 04 '14 at 15:26
  • @decPL: On the order of elements, it's a tricky one. Neither `Except`, nor `Intersect` guarantee preserving order, if I didn't overlook that in the docs. It's a consequence of the easiest algorithm to implement them, but it doesn't seem to be guaranteed. Also any duplicate elements from `list` will be collapsed into single elements, as the result is logically a set. So `[1, 2, 1, 3].Except([2]).Intersect([1, 3])` will yield `[1, 3]`, so I'm not sure it's appropriate to talk about result order here. – Joey Nov 04 '14 at 15:43
  • @Joey you are correct - I've based this on the implementation MS provided for both, which clearly preserves the order of elements, but indeed - there doesn't seem to be any 'contractual' guarantee that this will never change. Given that, I believe the correct answer is 'Yes, but...'. – decPL Nov 04 '14 at 15:46
  • 1
    Ah, `Intersect` guarantees order, but `Except` does not. – Joey Nov 04 '14 at 15:46
  • Which is funny, given how they're implemented (hint: quite similar). – decPL Nov 04 '14 at 15:47