I have two byte arrays with the exact same content. I tried:
if (bytearray1 == bytearray2) {...} else {...}
and
if (Array.Equals(bytearray1, bytearray2)) {....} else {...}
All time it goes to the else! I don't know why! I checked both arrays manually several times!!!
Try using the SequenceEqual
extension method. For example:
byte[] a1 = new byte[] { 1, 2, 3 };
byte[] a2 = new byte[] { 1, 2, 3 };
bool areEqual = a1.SequenceEqual(a2); // true
The ==
operator compares by reference; those are two different instances.
Array.Equals
is really Object.Equals
, which calls the instances Equals
method.
Since arrays do not override Equals()
, this too compares by reference.
Instead, you should call the LINQ SequenceEqual()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With