@PranavSingh: It will work for anything that has appropriate equality - so if your custom type overrides Equals(object) and/or implements IEquatable<T> it should be fine.
– Jon SkeetApr 8 '16 at 9:46

2

@k2ibegin: It uses the default equality comparer, which will use an IEquatable<T> implementation or the object.Equals(object) method. It sounds like you should create a new question with a minimal reproducible example - we can't really diagnose things in comments.
– Jon SkeetSep 5 '17 at 12:45

This method is implemented by using deferred execution. That means you could write for example:

var first10 = inListButNotInList2.Take(10);

It is also efficient since it internally uses a Set<T> to compare the objects. It works by first collecting all distinct values from the second sequence, and then streaming the results of the first, checking that they haven't been seen before.

Hmm. Not quite deferred. I'd say partially deferred. A complete Set<T> is built from the second sequence (i.e. it's fully iterated and stored), then items that can be added from the first sequence are yielded.
– spenderOct 9 '12 at 8:44

2

@spender, that's like saying that execution of Where is partially deferred because in list.Where(x => x.Id == 5) the value of the number 5 is stored at the start, rather than executed lazily.
– jwgJan 20 '14 at 14:35

This suffers from horrible performance, requiring a scan of the second list for every item in the first. Not downvoting because it works, but it's as bad as the original code.
– spenderOct 9 '12 at 8:46

Sometimes you only need to know if two lists are different, and not what those differences are. In that case, consider adding this extension method to your project. Note that your listed objects should implement IEquatable!

Whatever the Component class is, the methods shown here for Car should be implemented almost identically.

It's very important to note how we've written GetHashCode. In order to properly implement IEquatable, Equals and GetHashCodemust operate on the instance's properties in a logically compatible way.

Two lists with the same contents are still different objects, and will produce different hash codes. Since we want these two lists to be treated as equal, we must let GetHashCode produce the same value for each of them. We can accomplish this by delegating the hashcode to every element in the list, and using the standard bitwise XOR to combine them all. XOR is order-agnostic, so it doesn't matter if the lists are sorted differently. It only matters that they contain nothing but equivalent members.

Note: the strange name is to imply the fact that the method does not consider the order of the elements in the list. If you do care about the order of the elements in the list, this method is not for you!

as it's written here it would not even work for List<string> or List<int>, as for example the two lists 11;2;3 and 1;12;3 would be identical since you don't join the strings with some unique separator that isn't a possible item in the list. Apart from that, concatenating strings for a list with a lot of items is probably a performance killer.
– SwissCoderOct 5 '17 at 12:29

@SwissCoder: You are wrong, this is not a performacne killer for string. If you have two list with 50.000 strings (each of length 3) this algorithm needs 3 ms on my machine. The accepted answere needs 7. I think the trick is Jibz only needs one string comparision. Of course he has to add a unique separator.
– user1027167Feb 22 '18 at 12:30

@user1027167: Im not talking about comparing strings directly (as this is also not the question). Calling the .ToString() method of all the objects in a List with 50.000 objects can create a huge string, depending how it's implemented. I don't think that's the way to go. Then it's also risky to rely on a character or string being "unique", the code would not really be reusable like that.
– SwissCoderFeb 22 '18 at 13:02

Ok that's true. The questioner asked for the quickest way without giving the datatype of his lists. Probably this answere is the quickest way for the use case of the questioner.
– user1027167Feb 22 '18 at 13:47