How to compare multidimensional arrays with equality?

I know that you can use Enumerable.SequenceEqual to check for equality. But a multidimensional array does not have such a method. Any suggestion on how to compare a 2 dimensional array?

Actual problem:

public class SudokuGrid
{
    public Field[,] Grid
    {
        get { return grid; }
        private set { grid = value; }
    }
}

public class Field
{
    private byte digit;
    private bool isReadOnly;
    private Coordinate coordinate;
    private Field previousField;
    private Field nextField;
}

All these properties are set in the constructor SudokuGrid. Thus, all these properties have private setters. I want to save it that way.

Now I am doing some tests with C # unit tests. I want to compare 2 Gridswith their values, not their references.

Since I install everything using private seters through the constructor. This equal override in the class SudokuGridis correct, but not what I need:

public bool Equals(SudokuGrid other)
{
    if ((object)other == null) return false;

    bool isEqual = true;

    for (byte x = 0; x < this.Grid.GetLength(0); x++) // 0 represents the 1st dimensional array
    {
        for (byte y = 0; y < this.Grid.GetLength(1); y++) // 1 represents the 2nd dimensional array
        {
            if (!this.Grid[x, y].Equals(other.Grid[x, y]))
            {
                isEqual = false;
            }
        }
    }

    return isEqual;
}

, , . , :

SudokuGrid actual = new SudokuGrid(2, 3);

:

SudokuGrid expected = new SudokuGrid(2, 3);

:

Field[,] expected = sudoku.Grid;

grid, , setter . , , .

:

  • ? ( , , , ?)
  • ?
+5
1

, Field IComparable

public static bool ContentEquals<T>(this T[,] arr, T[,] other) where T : IComparable
{
    if (arr.GetLength(0) != other.GetLength(0) ||
        arr.GetLength(1) != other.GetLength(1))
        return false;
    for (int i = 0; i < arr.GetLength(0); i++)
        for (int j = 0; j < arr.GetLength(1); j++)
            if (arr[i, j].CompareTo(other[i, j]) != 0)
                return false;
    return true;
}
+2
source

All Articles