public static bool SequenceEqual(byte[] first, byte[] second)
{
if (first is null && second is null) return true;
if (first is null || second is null) return false;
return MemoryExtensions.SequenceEqual<byte>(first, second);
}
The Span solution proposed by @Joe Amenta above is very good. However it is not correct if either array can be null. My implementation handles nulls.
For those curious why the other answer doesn't work with nulls...
When casting a null byte[]
to a Span
, it results in a zero-length span. Therefore null and zero-length arrays both result in a zero-length span which will evaluate as equal even though the source arrays are not equal.