Sequence equality of object arrays containing two dimensional object arrays
14:29 09 Nov 2019

I wanted to code a function doing something with an input object[,] and returning an object[] whose elements are object[,]'s. I just coded something stupid for a start with no input to first set up tests, and then I will properly code the function :

public static class TestData
{
    public static object[,] Island1()
    {
        object[,] res = new object[3, 2];
        res[0, 0] = 1;
        res[0, 1] = 0;
        res[1, 0] = 1;
        res[1, 1] = 1;
        res[2, 0] = 1;
        res[2, 1] = 0;
        return res;
    }
}

public class ComponentsFinder
{
    public object[] GetIslands()
    {
        return  new object[]{TestData.Island1()};
    }
}

and the test :

[TestClass]
public class TestCompomentsFinder
{
    [TestMethod]
    public void FirstTest()
    {
        object[,] island1 = TestData.Island1();
        object[] expected = new object[] {island1};
        object[] actual = new ComponentsFinder().GetIslands();
        bool res = actual.SequenceEqual(expected);
        Assert.IsTrue(res);
    }
}

This test fails and I know why : even if both object[]'s contain only one object representing an object[,] that represents the same "matrix", they do not point to the same array, hence the failure of the test.

Would I have a real class C instead of object[,] it wouldn't be a problem as I would make C implement IEquatable properly, and the Equals override would then be called by SequenceEquals and the test would be ok.

But here I don't have a class, so what should I do ? Should I really wrap everything in classes or is there another way to test the equality of my object[]'s (which amounts to know to test equality of object[,]'s in the sense of having same dimensions and same respective coefficients, the coefficients of my object[,]'s are type implementing IEquatable) ?

c# arrays equality