You can use the Enumerable.Except()
method. This uses a comparer (either a default or one you supply) to evaluate which objects are in both sequences or just one:
var sequenceA = new[] { "a", "e", "i", "o", "u" };
var sequenceB = new[] { "a", "b", "c" };
var sequenceDiff = sequenceA.Except( sequenceB );
If you want to perform a complete disjunction of both sequences (A-B) union (B-A)
, you would have to use:
var sequenceDiff =
sequenceA.Except( sequenceB ).Union( sequenceB.Except( sequenceA ) );
If you have a complex type, you can write an IComparer<T>
for your type T and use the overload that accepts the comparer.
For the second part of your question, you would need to roll your own implementation to report which properties of a type are different .. there's nothing built into the .NET BCL directly. You have to decide what form this reporting would take? How would you identify and express differences in a complex type? You could certainly use reflection for this ... but if you're only dealing with a single type I would avoid that, and write a specialized differencing utility just for it. If yo're going to support a borad range of types, then reflection may make more sense.