[C#] How do I create and use multi-dimensional arrays in C#?

To create and use multi-dimensional arrays in C#, you can follow these steps:

  1. Declaring a multi-dimensional array:

    1dataType[,] arrayName = new dataType[rowSize, columnSize];
    
  2. Initializing values in the array:

    1arrayName[rowIndex, columnIndex] = value;
    
  3. Accessing values from the array:

    1dataType value = arrayName[rowIndex, columnIndex];
    
  4. Example of creating and using a multi-dimensional array:

     1// Declare and initialize a 2D array
     2int[,] matrix = new int[3, 3];
     3
     4// Assign values to the array
     5matrix[0, 0] = 1;
     6matrix[0, 1] = 2;
     7matrix[0, 2] = 3;
     8matrix[1, 0] = 4;
     9matrix[1, 1] = 5;
    10matrix[1, 2] = 6;
    11matrix[2, 0] = 7;
    12matrix[2, 1] = 8;
    13matrix[2, 2] = 9;
    14
    15// Access values from the array
    16int value1 = matrix[1, 1]; // value1 = 5
    17int value2 = matrix[2, 0]; // value2 = 7
    

Note: The number of dimensions in an array can be increased or decreased as needed. For example, you can create a 3D array by declaring dataType[,,] arrayName = new dataType[size1, size2, size3]; and access values using arrayName[index1, index2, index3];.