C# 如何比较两个数组列表?

C# 如何比较两个数组列表?,c#,arrays,list,byte,C#,Arrays,List,Byte,我有以下代码: 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, 0x

我有以下代码:

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);
List list1=新列表();
添加(新字节[]{0x41,0x41,0x41,0x41,0x78,0x56,0x34,0x12});
List list2=新列表();
添加(新字节[]{0x41,0x41,0x41,0x41,0x78,0x56,0x34,0x12});
添加(新字节[]{0x42,0x42,0x42,0x42,0x78,0x56,0x34,0x12});//这个数组
IEnumerable list3=list2。除了(list1);
我希望list3只包含list2中的字节[]数组,而不包含list1(标记为“this array”的数组),但它只返回list2中的所有字节[]。因此,我尝试了以下方法:

List<byte[]> list3 = new List<byte[]>();
foreach (byte[] array in list2)
    if (!list1.Contains(array))
        list3.Add(array);
List list3=新列表();
foreach(列表2中的字节[]数组)
如果(!list1.Contains(数组))
列表3.Add(数组);

但这让我得到了同样的结果。我做错了什么?

您的列表只包含一个元素。它们中的每一个都包含一个字节数组,并且这些字节数组彼此不同,这就是为什么除了之外的
和您的实现返回相同的结果

我不是c#专家,但您可以尝试定义以下列表:

List<byte> list1 = new List<byte>();
List list1=新列表();

之外的
包含
调用对象的
等于
方法。但是,对于数组,
Equals
只是执行引用相等检查。要比较内容,请使用
SequenceEqual
扩展方法

您必须在循环中更改支票:

List<byte[]> list3 = new List<byte[]>();
foreach (byte[] array in list2)
    if (!list1.Any(a => a.SequenceEqual(array)))
        list3.Add(array);
List list3=新列表();
foreach(列表2中的字节[]数组)
如果(!list1.Any(a=>a.SequenceEqual(数组)))
列表3.Add(数组);

使用Equals函数。假设
cont\u stream
是一个字节数组,那么

bool b = cont_stream[1].Equals(cont_stream[2]);

我怀疑它可能是这样的,但如果不循环遍历每个数组中的每个字节,就找不到任何简单的方法来正确处理它。谢谢!