C#select不会更改数组的值

时间:2018-02-25 17:48:01

标签: c# arrays select value-type reference-type

任何人都可以向我解释为什么这段代码会更改Matrix内部数组:

    public Labyrinth(int width, int height)
    {
        baseMatrix = new char[width][];

        for (int i = 0; i<baseMatrix.Length; ++i)
        {
            baseMatrix[i] = new char[height];
        }

        mod(baseMatrix[0]);
    }

    void mod(char[] x)
    {
        x[0] = 'a';
    }

这并没有改变任何事情:

    public Labyrinth(int width, int height)
    {
        baseMatrix = new char[width][];

        for (int i = 0; i<baseMatrix.Length; ++i)
        {
            baseMatrix[i] = new char[height];
        }

        baseMatrix.Select(x => x[0] = 'a');
     }

我不明白,select和函数都采用char []元素,我认为这是传递值,然后x [0]应该在两种情况下修改,我错了吗?

0 个答案:

没有答案