Well, you could use:
public static bool ByteArraysEqual(byte[] b1, byte[] b2)
{
if (b1 == b2) return true;
if (b1 == null || b2 == null) return false;
if (b1.Length != b2.Length) return false;
for (int i=0; i < b1.Length; i++)
{
if (b1[i] != b2[i]) return false;
}
return true;
}
(I normally use braces for everything, but I thought I'd experiment with this layout style just for a change...)
This has a few optimisations which SequenceEqual
can't (or doesn't) perform - such as the up-front length check. Direct array access will also be a bit more efficient than using the enumerator.
Admittedly it's unlikely to make a significant difference in most cases...
You could possibly make it faster in unmanaged code by making it compare 32 or 64 bits at a time instead of 8 - but I wouldn't like to code that on the fly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…