[Solved] How to assign value in two dimensional array


You’re on the right track. Just assign a value.

int[,] matarix = new int[4, 5];

for (int x = 0; x < 4; x++) {
    for (int y = 0; y < 5; y++) {
        matarix[x, y] = VALUE_HERE;
    }
}

One recommendation I would make, would be to use Array.GetLength instead of hard coding your for loops. Your code would become:

int[,] matarix = new int[4, 5];

for (int x = 0; x < matarix.GetLength(0); x++) {
    for (int y = 0; y < matarix.GetLength(1); y++) {
        matarix[x, y] = VALUE_HERE;
    }
}

You pass in one of the array’s dimensions, and it tells you how many indices exist for that dimension.

solved How to assign value in two dimensional array