Enumerable.Except returns distinct items

Yan Cui

I help clients go faster for less using serverless technologies.

This one took me by surprise a little but there’s something about the Enumerable.Except method which you ought to know about. At first you might have assumed (as I did) that given an array as the base set and calling the Except extension method with a second array will return all the items in the first array which do not appear in the second array.

Well, not exactly, for instance:

var list1 = new[] { 1, 1, 1, 1 };
var list2 = Enumerable.Empty<int>();

var result = list1.Except(list2); // returns { 1 } instead of the expected { 1, 1, 1, 1 }

var list3 = new[] { 1, 1, 2, 2, 3 };
var list4 = new[] { 1 };

var result2 = list3.Except(list4); // returns { 2, 3 } instead of the expected { 2, 2, 3 }

Does it surprise you that only distance instances of the original array is returned by the Except method? Whilst I haven’t found any official Microsoft documentation/article on why Except behaves in this way, the best explanation I have heard so far is that Except performs a set operation, according to MSDN:

Produces the set difference of two sequences by using the default equality comparer to compare values.

And a Set in the mathematical sense is defined as:

A set is a collection of distinct objects, considered as an object in its own right


Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game.
  2. Consulting: If you want to improve feature velocity, reduce costs, and make your systems more scalable, secure, and resilient, then let’s work together and make it happen.
  3. Join my FREE Community on Skool, where you can ask for help, share your success stories and hang out with me and other like-minded people without all the negativity from social media.

 

1 thought on “Enumerable.Except returns distinct items”

  1. Pingback: LINQ – Some pitfalls to look out for | theburningmonk.com

Leave a Comment

Your email address will not be published. Required fields are marked *