[TestMethod()]
public void TestExceptWithRandomInput()
{
byte[] listA = new byte[4096];
var rand = new Random();
rand.NextBytes(listA);
byte[] listB = new byte[] { 0x00 };
var nullCount = (from a in listA
where a == 0x00
select a);
var listC = listA.Except(listB);
Assert.AreEqual(4096, listA.Length);
Assert.AreEqual(4096 - nullCount.Count(), listC.Count()); //Fails!!
}
[TestMethod()]
public void TestWhereWithRandomInput()
{
byte[] listA = new byte[4096];
var rand = new Random();
rand.NextBytes(listA);
byte[] listB = new byte[] { 0x00 };
var nullCount = (from a in listA
where a == 0x00
select a);
var listC = listA.Where(a => !listB.Contains(a));
Assert.AreEqual(4096, listA.Length);
Assert.AreEqual(4096 - nullCount.Count(), listC.Count()); //Successful
}
上面的代码在使用 except() 函数时似乎失败,但在使用 Where() 时工作正常。似乎缺少什么?我需要为字节实现 IEqualityComparer 吗?我认为这仅对复杂类型是必需的。