Skip to content

Comparing Collections

Michael Brown edited this page May 21, 2019 · 1 revision

Comparing two collections with a single expected change:

var object1 = new TestObject { IntArray = new int[] { 0, 0, 5, 0, 0 } };
var object2 = new TestObject { IntArray = new int[] { 0, 0, 2, 0, 0 } };
var diff = object1.Diff(object2);
Assert.AreEqual(1, diff.Count);

Comparing two collections that are ordered differently (explicitly allowed):

var object1 = new TestObject { IntArray = new int[] { 1, 2, 3, 4, 5 } };
var object2 = new TestObject { IntArray = new int[] { 2, 1, 4, 5, 3 } };
var diff = object1.Diff(object2, ComparisonOptions.All | ComparisonOptions.AllowCollectionsToBeOutOfOrder);
Assert.AreEqual(0, diff.Count);

Comparing two dictionaries:

var object1 = new TestObject { IntDictionary = new Dictionary<int, string> { { 0, "1" }, { 1, "2" }, { 2, "3" }, { 3, "4" }, { 4, "5" } } };
var object2 = new TestObject { IntDictionary = new Dictionary<int, string> { { 0, "1" }, { 1, "2" }, { 2, "2" }, { 3, "4" }, { 4, "5" } } };
var diff = object1.Diff(object2);
Assert.AreEqual(1, diff.Count);