I have the following code:
List<byte[]> list1 = new List<byte[]>();
list1.Add(new byte[] { 0x41, 0x41, 0x41, 0x41, 0x78, 0x56, 0x34, 0x12 });
List<byte[]> list2 = new List<byte[]>();
list2.Add(new byte[] { 0x41, 0x41, 0x41, 0x41, 0x78, 0x56, 0x34, 0x12 });
list2.Add(new byte[] { 0x42, 0x42, 0x42, 0x42, 0x78, 0x56, 0x34, 0x12 }); // this array
IEnumerable<byte[]> list3 = list2.Except(list1);
I want list3 to only contain the byte[] arrays that are in list2 but not in list1 (the one marked “this array”), but instead it just returns all of list2. So then I tried the following:
List<byte[]> list3 = new List<byte[]>();
foreach (byte[] array in list2)
if (!list1.Contains(array))
list3.Add(array);
but this got me the same result. What am I doing wrong?
Both
ExceptandContainsinvokes the object’sEqualsmethod. For arrays, however,Equalssimply performs a reference equality check. To compare the contents, use theSequenceEqualextension method.You’ll have to change your check in your loop: