Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays of ValueTypes don't like object.Equals?

Tags:

arrays

c#

This had me pulling my hair out for a couple days:

byte[] _A = new byte[64];
// Fill _A with some meaningful, valid data.

byte[] _B = new byte[_A.Length];
_A.CopyTo( _B, 0 );

if( !_A.Equals( _B ) ) {
    throw new WtfException(
        "It appears object.Equals doesn't work on arrays of value types...");
}

Yes, that throws the WtfException. It took me a few days to notice. byte is a ValueType. However, byte[] is a System.Array, which is a reference type. As per the .NET documenation:

The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.

Can anybody help?

like image 871
Unknown Avatar asked Oct 19 '25 09:10

Unknown


2 Answers

You're comparing reference types (arrays) and not value types. _A and _B are indeed different - they are two different arrays that happen to contain the same values.

like image 96
zmbq Avatar answered Oct 21 '25 21:10

zmbq


_A and _B are not references to the same array. Thus, they are not equal. You need to make something like this:

private static bool ValueTypeArraysAreEqual( Array p_lhs, Array p_rhs ) {
if( p_lhs == null ) {
return p_rhs == null;
}
if( p_rhs == null ) {
return false;
}
if( p_lhs.Length != p_rhs.Length ) {
return false;
}
return Parallel.For( 0, p_lhs.Length, ( _lcv, loopState ) => {
if( !p_lhs.GetValue( _lcv ).Equals( p_rhs.GetValue( _lcv ) ) ) {
loopState.Break();
}
} ).IsCompleted;
}

you can use object.Equal in the loops because you can compare the ValueTypes that the Loop contains. The use of System.Threading.Tasks.Parallel helps me move things along a little quicker. Parallel.For returns an struct that tells you whether the loop was halted. If you never halted the loop with loopState.Break, then they all matched. If, for some reason, you can't use Parallel.For, just do a for loop that returns false;

like image 42
Unknown Avatar answered Oct 21 '25 21:10

Unknown