Enumerable.Except returns distinct items

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

 

Learn to build Production-Ready Serverless applications

Want to learn how to build Serverless applications and follow best practices? Subscribe to my newsletter and join over 5,000 AWS & Serverless enthusiasts who have signed up already.

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 *