can someone explain me why I do get different results?
bool result = true;
for (int row = 0; row < State.Rows; row++)
{
for (int col = 0; col < State.Cols; col++)
{
if (map[row][col] != other.map[row][col])
//if (!map[row][col].Equals(other.map[row][col]))
result = false;
}
}
bool sequenceEqual = map.SequenceEqual(other.map);
//result = true
//sequenceEqual = false
I overwrote Equals(), GetHashCode(), == and != for the type to be compared. I also tried the CollectionComparer from this answer, and got the same result.
They’re not equivalent.
SequenceEqualusesEqualsfor every element of a sequence. Sincemapis a two dimensional array, each “element” is actually a row, and that means that it’s theEqualsmethod ofArraythat ends up being called.Your method compares each element directly using
!=, which is what you want.A solution would to supply a custom equality comparer to SequenceEquals. Said comparer would then call SequenceEquals again.