This question already has an answer here:
- Comparing two byte arrays in .NET 28 answers
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!!!
The
==
operator compares by reference; those are two different instances.Array.Equals
is reallyObject.Equals
, which calls the instancesEquals
method.Since arrays do not override
Equals()
, this too compares by reference.Instead, you should call the LINQ
SequenceEqual()
method.Both the
==
operator and the Equals method will test reference equality. Since you have two separate arrays, they will never be equal.Since you want to test that both arrays have the same content in the same order, try using the
SequenceEqual
method instead.Try using the
SequenceEqual
extension method. For example:As an alternative, if you are not comfortable using LINQ you can use the System.Convert class...